b.cpp

#include"a.h"
#include"a.cpp"

int main(){}

a.cpp

#include"a.h"

a::a(): b(0) { }

a.h

#ifndef A
#define A
class a{
	int b;
	a();
};
#endif

Getting; "1>b.obj : error LNK2005: "private: __thiscall a::a(void)" (??0a@@AAE@XZ) already defined in a.obj" when trying to compile.

Using Visual Studio 2010, Windows 7 64bits

Wheres my mistake?

Tyvm for your anwsers.

Recommended Answers

All 3 Replies

My guess is that you have a project that contains both a.cpp and b.cpp, then you have included a.cpp in b.cpp. You can't do that. In b.cpp remove the line that includes a.cpp.

It works and compiles if I comment out " #include"a.cpp" " in b.cpp, tyvm.

I guess the IDE does all those kinds of inclusions, hence if I add mine it includes the file twice, right?

You need to understand how the IDE works -- the project contains both a.cpp and b.cpp. The IDE calls the compiler for each of those *.cpp files, then links them together with other libraries to generate the *.exe file. That's one reason you should never include one *.cpp file in another.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.