0
votes

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!!!

5

5 Answers

5
votes
#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.

2
votes

In main.cpp, you should include the header file, rather than the cpp file.

Change

#include "addr.cpp"

To

#include "addr.h"
2
votes

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.

0
votes

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.

0
votes

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...