What is the correct syntax for having function implementation in a separate file? For example:
foo.h
int Multiply(const int Number);
foo.cpp
#include "foo.h"
int Multiply(const int Number)
{
return Number * 2;
}
I see this used a lot, but when I try it I get an error having to do with a missing main() function. I get the error even when I try to compile working code.
Roughly speaking, you need to have a main() function inside one of your C++ files you are compiling.
As the compiler says, you just need to have a main() method inside your foo.cpp, like so:
#include "foo.h"
#include <iostream>
using namespace std;
int Multiply(const int Number)
{
return Number * 2;
}
int main() {
// your "main" program implementation goes here
cout << Multiply(3) << endl;
return 0;
}
Or you could separate your main function into a different file, like so (omit the main() block in foo.cpp if you intend to do this):
main.cpp
#include "foo.h"
#include <iostream>
using namespace std;
int main() {
cout << Multiply(3) << endl;
return 0;
}
Then compile it like
g++ main.cpp foo.cpp
Every program in C++ is a collection of one or more translation units, aka source files.
After these files get compiled, the linker searches for the entry point of your program aka the int main() function. Since it fails to find it it gives you an error.
Don't forget that building the program is yielding an executable file. An executable file without an entry point is nonsense.
Related
When I want to run the code the compiler says undefined reference to math::calc
I read questions and answers about this problem at StackOverflow and it do not help me solve my problem.
Comp.h
#include <utility>
namespace math {
typedef std::pair<double,double> Comp;
double getFirst(Comp a);
...
}
Comp.cpp
#include "comp.h"
namespace math {
double getFirst(Comp a) {
return a.first;
}
...
}
Comp file: every function return Comp or double. I call function from cal.cpp file several times
cal.h
#include "comp.h"
#include "helper.h"
namespace math {
Comp calc(const string& str);
...
}
cal.cpp
#include "eval.h"
namespace math {
Comp calc(const string& str) {
...
}
}
Cal file: Some functions return with comp type, not just the cal function.
helper.h
namespace math {
...
}
helper.cpp
#include "helper.h"
namespace math {
...
}
helper file just contains few function that I calling from cal.cpp file. Each function is calling several times.
main.cpp
#include "calc.h"
int main() {
string str = " ";
pair<double,double> res = math::calc(str);
cout << res.first << " " << res.second << endl;
return 0;
}
In the whole project, I do not use any classes.
I included every file that I calling except the c++ original file.
I use the std namespace in each file but I do not write it here.
I absolutely no idea what could be the problem with my code.
If I also include cal.cpp in the main.cpp the code editor says undefined reference to each file that I calling from helper.h. I do not want to include cal.cpp in the main file I just mentioned it
You have to compile all your project's *.cpp files and then link them (results of compilation) properly - the linking process depends on the environment/IDE you're using, just look it up. If you're not linking it properly the final executable wont have all the functions definitions that requires, hence getting undefined reference.
I'm trying to learn how to utilize header files in C++ projects, so I made .cpp files containing simple functions to make sure I'm doing all the declaring and including correctly.
Everything worked fine when I only had one set of .cpp and .h files, but when I try to add more I get errors.
To start with, in my project I had:
helloworld.cpp
#include "helloworld.h"
#include <iostream>
#include <cstdio>
using namespace std;
int HelloWorld() {
puts("Hello, World!");
cout << "Hello, World!" << endl;
return 0;
}
helloworld.h
#ifndef HELLOWORLD_H_INCLUDED
#define HELLOWORLD_H_INCLUDED
int HelloWorld();
#endif /* HELLOWORLD_H_INCLUDED */
main.cpp
#include "helloworld.h"
#include <iostream>
using namespace std;
int main(){
HelloWorld();
return 0;
}
Which built with no errors and ran correctly.
Next I tried adding a second .cpp and .h file, which created building errors.
pointers.cpp
#include "pointers.h"
#include <iostream>
using namespace std;
int Pointers() {
int x = 1;
int *ptr_a = &x;
cout << *ptr_a << endl;
return 0;
}
pointers.h
#ifndef POINTERS_H_INCLUDED
#ifndef POINTERS_H_INCLUDED
int Pointers();
#endif /* POINTERS_H_INCLUDED */
and modified main.cpp:
#include "helloworld.h"
#include "pointers.h"
#include <iostream>
using namespace std;
int main(){
HelloWorld();
Pointers();
return 0;
}
Now when I try to build, I get an error saying there are multiple definitions of main -- one in main.cpp, and the other in pointers.cpp.
Even more oddly, if I make a new project and do the exact same thing but reverse the order in which I create the .cpp and .h files (i.e. pointers first then helloworld), it builds and runs correctly with just the pointers files but runs into the same error when adding helloworld files, saying that the multiple exceptions of main are in main.cpp and helloworld.cpp.
I figure it must have something to do with Eclipse itself, but I don't know what the exact issue is.
Does anyone know what might be going on?
I am using the g++ compiler on a linux mint.
I´m tying to make a class for neural Network witch i want play tic tac toe.
My Header:
#ifndef tttAi
#define tttAi
#include <string>
class Synaps{
public:
explicit Synaps(const std::string& n, double v);
void add(double ad);
void multi(double mu);
void save();
double read();
private:
std::string name;
double Syn_value;
};
#endif
My functions are:
#include "tttAi.h"
#include <fstream>
#include <string>
Synaps::Synaps(const std::string& n, double v)
:name(n), Syn_value(v){
}
void Synaps::add(double ad) { //change by addition
Syn_value += ad;
}
void Synaps::multi(double multip) { //change by multiplication
Syn_value *= multip;
}
double Synaps::read() {
return Syn_value;
}
And here is what i wanted it to do:
#include <iostream>
#include "tttAi.h"
#include "tttAi.cpp"
int main() {
Synaps n1n6("n1n6", 75);
n1n6.multi(2);
std::cout << n1n6.read() << '\n';
/*Want it to just output the value of Syn_value Witch at
this point should be 150 if i have done everything right*/
}
Command used: g++ -Wall -std=c++14 *cpp
so what i would think i´d get was just the consol output of 150 but whilst compiling i get this endless error message:
enter image description here
hope you have a idea of what i did wrong, any ideas welcome.
You're including a cpp file containing definitions multiple times (i.e. you should not write
#include "tttAi.cpp"
in your main.cpp file) and therefore violating the ODR - one definition rule.
Remember that including a file means duplicating that file's contents in the point of inclusion (and therefore duplicating your definitions as well).
The problem seems to be #include "tttAi.cpp" in your main.cpp file.
You should not include cpp files
You should not #include .cpp files. Instead, just list them on the command line when you compile.
Add.cpp
int add(int x, int y)
{
return x + y;
}
Main.cpp
#include <iostream>
int main()
{
using namespace std;
cout << "The sum of 3 and 4 is: " << add(3, 4) << endl;
return 0;
}
When I try to compile this program I get an error message for line 6 of main.cpp that states: "error: 'add' was not declared in this scope".
Create a header file
Contents:
int add(int x, y);
Include that file main.cpp
i.e. #include "headerfile.h"
Then the rest is up to the compiler environment. Basically need to compile each .cpp to object code and then link them. You need to read up about this as this differs between environment. Also read up on header guards and also stuff like graadle, SCONS, Makefiles. Also good to learn about version control systes e.g. mercurial.
Guess you going to have a busy day
You need Add.h file and include it in your Main.cpp
Add.h
int add(int x, int y);
Main.cpp
#include <iostream>
#include "Add.h"
...
In c++ the scope is all visible functions/methods and variables. In order for it to be seen in the scope in this instance you would have to create a header file that contains your method "add". One way to do this would be to instead of having it in a .cpp file have it in a .h file, then include that .h file in your main.cpp file like this
#include "Add.h"
I have 1 cpp file with main().
It relies on structs and functions in another (say, header.hpp).
The structs are defined in header.hpp, along with function prototypes. The functions are implemented in header.cpp.
When I try to compile, I get an error message saying:
undefined reference to `see_blah(my_thing *)`
So to give an overview:
header.hpp:
#ifndef HEADERDUR_HPP
#define HEADERDUR_HPP
struct my_thing{
int blah;
};
int see_blah(my_thing*);
#endif
header.cpp:
#include "header.hpp"
int see_blah(my_thing * thingy){
// ...
}
main.cpp:
#include <iostream>
#include "header.hpp"
using namespace std;
int main(void)
{
thinger.blah = 123;
cout << see_blah(&thinger) << endl;
return 0;
}
I have no idea what I'm doing wrong, and I can't find any answers. Thanks for any answers, they are very much appreciated!
You should be aware that you're missing a semi-colon at the end of your structure definition. This means it's folding the two (supposedly separate) parts together and that you're not getting the function prototype as a result.
The following compiles fine (after fixing a couple of other errors as well):
// main.cpp
#include <iostream>
#include "header.hpp"
using namespace std; // <- not best practice, but irrelevant here :-)
int main(void)
{
my_thing thinger; // <- need this!
thinger.blah = 123;
cout << see_blah(&thinger) << endl;
return 0;
}
// header.cpp
#include "header.hpp"
int see_blah(my_thing * thingy){
// ...
}
// header.hpp
#ifndef HEADERDUR_HPP
#define HEADERDUR_HPP
struct my_thing{
int blah;
}; // <- see here.
int see_blah(my_thing*);
#endif
with:
g++ -o progname main.cpp header.cpp
gcc actually gave an error with that code you posted so I'm not certain why your compiler didn't. That command line above is also important - if you're compiling and linking in one step, you need to provide all required C++ source files (otherwise the linker won't have access to everything).
Your code is fine. You're just compiling it wrong. Try:
g++ main.cpp header.cpp
You need to:
#include "header.hpp"
in your *main.cpp file.
If you have included header.hpp, than probably you haven't link it(header.cpp) with main.cpp. What environment are you using(g++ or VC++)?
Edit:for linking in g++ you must write:
g++ main.cpp header.cpp -o program
Also you are missing semicolon in the end of your struct!
thinger.blah = 123; should be along the lines of:
my_thing thinger = { 123 };
in addition to issues other posters have mentioned. please, update your example so it compiles.
You are missing a semi colon at the end of your structure definition and mixing it with the method.