include problem

hi all,

i have 3 files: classes.h (just class declerations), classes.cpp(function definitions of the classes in classes.h) and main.cpp... classes.cpp includes classes.h and main.cpp includes classes.cpp only. when i compile i receive redefinition error. what should i do? Here is the 2 errors:

---------------------------------------------------------------------
1>cylApp.obj : error LNK2005: "public: __thiscall Cube::Cube(void)" (??0Cube@@QAE@XZ) already defined in classes.obj
1>cylApp.obj : error LNK2005: "public: virtual void __thiscall Cube::Draw(void)" (?Draw@Cube@@UAEXXZ) already defined in classes.obj
---------------------------------------------------------------------

====================================
classes.h is below
====================================
#include <glut.h>

class Scene3D
{
};

class Primitive3D
{
public:
GLdouble x,y,z;
bool isSelected;
virtual void Draw() = 0;
};

class Cube : public Primitive3D
{
public:
Cube();
void Draw();
};

====================================
classes.cpp is below
====================================
#include "classes.h"

Cube::Cube ()
{
}

void Cube::Draw()
{
}

====================================
main.cpp is below
====================================
#include "classes.cpp"

void main ()
{
}

====================================
main.cpp is cylApp.cpp in fact, sorry...
#include "classes.cpp"

You shouldn't include cpp files, but header files
But i think i see some examples which includes .cpp files... Bad memory or is it possible?
should i convert classes.cpp to classesFnc.h?
Bad memory or bad examples
should i convert classes.cpp to classesFnc.h?
No, just include classes.h

Note:
It's possible to include any file (so also a cpp file) #include "something" inerts the file contents in that point of the code.
If you are including a cpp file and passing it to the compiler, its code would be compiled twice generating linker errors as symbols defined in it will result to be defined more than once.
For this reason you should only include header files
Last edited on
thanx for the answer, so the "classes.cpp" is compiled separately and when I include it to "main.cpp" it causes linker problems... Am I wrong?
That is correct. When you #include classes.cpp, it puts all of the function bodies in main.cpp as well as in classes.cpp. Therefore the linker finds two seperate function bodies and it doesn't know which one to use.

Also, obligatory link:

http://cplusplus.com/forum/articles/10627/
classes.cpp must include classes.h

main.cpp must include classes.h

compile and link classes.cpp and cmain.cpp

thanx all :)
Topic archived. No new replies allowed.