I'm trying to create a class of complex numbers in C++, but I'm getting a strange error for the initialiser of the class here is a screenshot of the error :
And here is my code :
main.cpp :
#include <iostream>
#include "cmath"
#include "Complexx.h"
int main(int argc, const char * argv[])
{
complex c(1,1);
std::cout << c.realPart << "+" <<c.imaginaryPart << "i";
return 0;
}
Complexx.h :
#ifndef Functions_Complexx_h
#define Functions_Complexx_h
#include "cmath"
using namespace std;
class complex {
public:
double imaginaryPart;
double realPart;
complex(double realPart, double imaginaryPart);
double modulus();
};
#endif
What am I doing wrong here?
The problem is here.
complex(double realPart, double imaginaryPart);
You've declared a constructor, but you haven't defined it. That's why the linker is complaining about undefined symbol complex::complex(double, double)
This is probably the definition you want - to store those two parameters in the object.
complex(double realPart, double imaginaryPart)
: realPart(realPart)
, imaginaryPart(imaginaryPart)
{ }
It doesn't matter what those parameters are named. C++ will not infer that because they have the same names as your member variables that they should be copied to the object.
You haven't provided a definition for the complex(double, double) constructor, although a declaration is provided. This is what the linker complains about.
Add this to a .cpp file in your project which #includes your header file complexx.h (possibly complexx.cpp):
complex::complex(double realPart, double imaginaryPart)
:
realPart(realPart),
imaginaryPart(imaginaryPart)
{ }
It is possible that the same problem exists for double modulus(), for which no linker error is issued because you are not invoking it (yet).
It seems that you haven't linked the definition of the complex(double, double) constructor.
This may be caused by the fact that you forgot to link Complexx.cpp (assuming the name of the cpp file is the same as the header file) or that you totally forgot to define the constructor inside the linked Complexx.cpp file.
Just run this g++ commands when compiling main.cpp (assuming you are using gcc):
g++ -c Complexx.cpp -o Complexx.o
g++ -c main.cpp -o main.o
g++ main.o Complexx.o -o app
Related
I am using a simple function to add to integers, the class is declared in the Adder.h file as below
class Adder
{
public:
int add (int x, int y);
};
Then I have the Adder.cpp file which has the function definition
int add (int x, int y)
{
return x + y;
}
Then the main.cpp file which calls the function
# include "Adder.h"
# include <iostream>
using namespace std;
int main()
{
Adder adder1;
int result = adder1.add (2, 3);
cout << result;
}
I ran g++ -c Adder.cpp to create Adder.o file beforehand.
Then I ran g++ main.cpp but go the following error
main.cpp:(.text+0x2d): undefined reference to `Adder::add(int, int)'
Where am I going wrong?
In your second and final step, you didn't instruct the compiler (linker more exactly) to take into account Adder.o, so your final executable still doesn't know the implementation of Adder::add
Try, after getting Adder.o, to run g++ main.cpp Adder.o
Also, this may be relevant : Difference between compiling with object and source files
Also, if that is the complete code, as others have pointed out, in the Adder.cpp, you are just defining a simple function, not the one from the Adder class.
The problem is that you've defined a free function named add instead of defining a member function of class Adder. To define add as a member function we have to be in the scope of the class Adder which we can do by adding Adder:: before add as shown below:
Adder.cpp
//note the use of scope resolution operator ::
int Adder::add(int x, int y)//define a member function instead of a free function
{
return x + y;
}
In the above modified code, we are defining the member function add instead of the free function add.
I am using a simple function to add to integers, the class is declared in the Adder.h file as below
class Adder
{
public:
int add (int x, int y);
};
Then I have the Adder.cpp file which has the function definition
int add (int x, int y)
{
return x + y;
}
Then the main.cpp file which calls the function
# include "Adder.h"
# include <iostream>
using namespace std;
int main()
{
Adder adder1;
int result = adder1.add (2, 3);
cout << result;
}
I ran g++ -c Adder.cpp to create Adder.o file beforehand.
Then I ran g++ main.cpp but go the following error
main.cpp:(.text+0x2d): undefined reference to `Adder::add(int, int)'
Where am I going wrong?
In your second and final step, you didn't instruct the compiler (linker more exactly) to take into account Adder.o, so your final executable still doesn't know the implementation of Adder::add
Try, after getting Adder.o, to run g++ main.cpp Adder.o
Also, this may be relevant : Difference between compiling with object and source files
Also, if that is the complete code, as others have pointed out, in the Adder.cpp, you are just defining a simple function, not the one from the Adder class.
The problem is that you've defined a free function named add instead of defining a member function of class Adder. To define add as a member function we have to be in the scope of the class Adder which we can do by adding Adder:: before add as shown below:
Adder.cpp
//note the use of scope resolution operator ::
int Adder::add(int x, int y)//define a member function instead of a free function
{
return x + y;
}
In the above modified code, we are defining the member function add instead of the free function add.
This question may seem similar to other "undefined reference error when accessing static class member" questions. I have explored them and what I understood is I need to define the static class member separately in a source file, so that an object is created for the static class member that holds data.
My problem is that I am following definition rules but still get the undefined reference errors.
problem can be reproduced using this code:
main.cpp
#include <iostream>
#include "src/a.hpp"
int main() {
std::cout << a::x;
return 0;
}
src/a.hpp
class a {
public:
static int x;
};
src/a.cpp
#include "a.hpp"
int a::x = 20;
I compile main.cpp using g++ main.cpp -o main. I have a test directory that has main.cpp and a sub directory src, src contains a.hpp and a.cpp.
The error resolves if i define the static variable within the header file, but other posts suggest it should lead to linker errors.
The problem was in the compile command I used, g++ main.cpp -o main does not compile src/a.cpp. Compile it with g++ main.cpp src/a.cpp -o main and it works fine.
I am trying to relearn C++ after taking an intro course a few years ago and I’m having some basic problems. My current problem occurs when trying to use a friend function. Here is my code in 2 files.
First:
// fun.cpp
#include <iostream>
using namespace std;
class classA {
friend void funct();
public:
classA(int a=1,int b=2):propa(a),propb(b){cout<<"constructor\n";}
private:
int propa;
int propb;
void outfun(){
cout<<"propa="<<propa<<endl<<"propb="<<propb<<endl;
}
};
void funct(){ // ERROR HERE
cout<<"enter funct"<<endl;
classA tmp(1,2);
tmp.outfun();
cout<<"exit funct"<<endl;
}
Second:
// mainfile.cpp
#include <iostream>
#include "fun.cpp"
using namespace std;
int main(int nargin,char* varargin[]) {
cout<<"call funct"<<endl;
funct();
cout<<"exit main"<<endl;
return 0;
}
The error I am getting is "multiple definition of `funct()'". Am I using the wrong syntax when declaring it as a friend function?
Here is a highly simplified but hopefully relevant view of what happens when you build your code in C++.
C++ splits the load of generating machine executable code in following different phases -
Preprocessing - This is where any macros - #defines etc you might be using get expanded.
Compiling - Each cpp file along with all the #included files in that file directly or indirectly (together called a compilation unit) is converted into machine readable object code.
This is where C++ also checks that all functions defined (i.e. containing a body in { } e.g.
void Foo( int x){ return Boo(x); }) are referring to other functions in a valid manner.
The way it does that is by insisting that you provide at least a declaration of these other functions (e.g. void Boo(int); ) before you call it so it can check that you are calling it properly among other things. This can be done either directly in the cpp file where it is called or usually in an included header file.
Note that only the machine code that corresponds to functions defined in this cpp and included files gets built as the object (binary) version of this compilation unit (e.g. Foo) and not the ones that are merely declared (e.g. Boo).
Linking - This is the stage where C++ goes hunting for stuff declared and called in each compilation unit and links it to the places where it is getting called. Now if there was no definition found of this function the linker gives up and errors out. Similarly if it finds multiple definitions of the same function signature (essentially the name and parameter types it takes) it also errors out as it considers it ambiguous and doesn't want to pick one arbitrarily.
The latter is what is happening in your case. By doing a #include of the fun.cpp file, both fun.cpp and mainfile.cpp have a definition of funct() and the linker doesn't know which one to use in your program and is complaining about it.
The fix as Vaughn mentioned above is to not include the cpp file with the definition of funct() in mainfile.cpp and instead move the declaration of funct() in a separate header file and include that in mainline.cpp. This way the compiler will get the declaration of funct() to work with and the linker would get just one definition of funct() from fun.cpp and will use it with confidence.
The problem is that if you include fun.cpp in two places in your program, you will end up defining it twice, which isn't valid.
You don't want to include cpp files. You want to include header files.
The header file should just have the class definition. The corresponding cpp file, which you will compile separately, will have the function definition.
fun.hpp:
#include <iostream>
class classA {
friend void funct();
public:
classA(int a=1,int b=2):propa(a),propb(b){std::cout<<"constructor\n";}
private:
int propa;
int propb;
void outfun(){
std::cout<<"propa="<<propa<<endl<<"propb="<<propb<< std::endl;
}
};
fun.cpp:
#include "fun.hpp"
using namespace std;
void funct(){
cout<<"enter funct"<<endl;
classA tmp(1,2);
tmp.outfun();
cout<<"exit funct"<<endl;
}
mainfile.cpp:
#include <iostream>
#include "fun.hpp"
using namespace std;
int main(int nargin,char* varargin[]) {
cout<<"call funct"<<endl;
funct();
cout<<"exit main"<<endl;
return 0;
}
Note that it is generally recommended to avoid using namespace std in header files.
This problem happens because you are calling fun.cpp instead of fun.hpp. So c++ compiler finds func.cpp definition twice and throws this error.
Change line 3 of your main.cpp file, from #include "fun.cpp" to #include "fun.hpp" .
You have #include "fun.cpp" in mainfile.cpp so compiling with:
g++ -o hw1 mainfile.cpp
will work, however if you compile by linking these together like
g++ -g -std=c++11 -Wall -pedantic -c -o fun.o fun.cpp
g++ -g -std=c++11 -Wall -pedantic -c -o mainfile.o mainfile.cpp
As they mention above, adding #include "fun.hpp" will need to be done or it won't work. However, your case with the funct() function is slightly different than my problem.
I had this issue when doing a HW assignment and the autograder compiled by the lower bash recipe, yet locally it worked using the upper bash.
I cannot get this to work
Addr.h
#ifndef ADDR_H
#define ADDR_H
class Foo{
public:
Foo(); // function called the default constructor
Foo( int a, int b ); // function called the overloaded constructor
int Manipulate( int g, int h );
private:
int x;
int y;
};
#endif
Addr.cpp
#include "addr.h"
Foo::Foo(){
x = 5;
y = 10;
}
Foo::Foo( int a, int b ){
x = a;
y = b;
}
int Foo::Manipulate( int g, int h ){
return x = h + g*x;
}
main.cpp
#include "addr.cpp"
int main(){
Foo myTest = Foo( 20, 45 );
while(1){}
return 0;
}
What am i doing wrong? I get these linker errors:
error LNK2005: "public: int __thiscall Foo::Manipulate(int,int)" (?Manipulate#Foo##QAEHHH#Z) already defined in addr.obj c:\Users\christian\documents\visual studio 2010\Projects\Console Test\Console Test\main.obj
error LNK2005: "public: __thiscall Foo::Foo(int,int)" (??0Foo##QAE#HH#Z) already defined in addr.obj c:\Users\christian\documents\visual studio 2010\Projects\Console Test\Console Test\main.obj
error LNK2005: "public: __thiscall Foo::Foo(void)" (??0Foo##QAE#XZ) already defined in addr.obj c:\Users\christian\documents\visual studio 2010\Projects\Console Test\Console Test\main.obj
error LNK1169: one or more multiply defined symbols found c:\users\christian\documents\visual studio 2010\Projects\Console Test\Release\Console Test.exe
I would appreciate any kind of help!!!
#include "addr.cpp"
You're including the .cpp file to your main.cpp, which is what causing the problem. You should include the header file instead, as:
#include "addr.h" //add this instead, to your main.cpp
Because its the header file which is containing the definition of the class Foo.
In main.cpp, you should include the header file, rather than the cpp file.
Change
#include "addr.cpp"
To
#include "addr.h"
In main.cpp
#include "addr.cpp"
should be
#include "addr.h"
If you #include "addr.cpp", you can compile main.cpp (and even link it into an executable on it's own), but when you link main.obj and add.obj, you get a duplicate definition error, because class Foo is now fully defined in both object files.
You can either #include "addr.h" (as already stated), or make the build system not compile addr.cpp. The former is usually preferred, though the latter would also solve the trouble and is often used with automatic code generation tools' output.
There are two ways of getting the class data into main.cpp
1) include the header file "addr.h" in the main.cpp file, so now you have the class and member functions' definitions, but don't have the implementation part of them. So while compiling, the code should be :
g++ addr.cpp main.cpp.
2) include the cpp file "addr.cpp" in the main.cpp, so you automatically have the definitions also as "addr.h" is included in "addr.cpp". Now you must compile only the main.cpp file: g++ main.cpp.
First method is standard and is a good programming practice for maintaining modularity and encapsulation...