Sign in to follow this  
ChainChief

namespace error

Recommended Posts

ChainChief    122
I just started learning how to use namespaces, and I keep getting a linking error even though the code is set up exactly the same as I see it in the tutorials. I have this in a seperate header file included in "stdafx.h" > namespace jGroup { // class CTest { protected: int iVal; public: CTest () {iVal = 0;}; ~CTest () {}; int GetVal () {return iVal;}; }; // void DoStuff () ; } void jGroup::DoStuff () { } And in main(), I have this > #include "stdafx.h" int _tmain(int argc, _TCHAR* argv[]) { jGroup::CTest T1; jGroup::DoStuff (); cout << T1.GetVal (); system ("Pause"); return 0; } And I get the following error messages: 1>Linking... 1>stdafx.obj : error LNK2005: "void __cdecl jGroup::DoStuff(void)" (?DoStuff@jGroup@@YAXXZ) already defined in JConsole.obj 1>C:\Users\Jacob\Programming\Projects\JConsole\Debug\JConsole.exe : fatal error LNK1169: one or more multiply defined symbols found I know there shouldnt be a redefinition error because "stdafx.h" is only included once. Does anyone know why I am getting these errors? Jacob

Share this post


Link to post
Share on other sites
rip-off    10976
You don't (yet) understand the C++ compilation process. Each source file is compiled in isolation, so even if a file can only be included once does not prevent multiple definition errors across multiple files. I would advise you to read this.

Share this post


Link to post
Share on other sites
ChainChief    122
Yes, I know this..
But the problem is not that there is another identifier with the same name in another file. I know how to organize files, I am just trying to show the error in the simplest way possible.

Share this post


Link to post
Share on other sites
rip-off    10976
Quote:
Original post by ChainChief
Yes, I know this..
But the problem is not that there is another identifier with the same name in another file. I know how to organize files, I am just trying to show the error in the simplest way possible.


Remember that #include is basically a copy and paste operation. What the compiler sees is the preprocessed translation unit, it doesn't have any distinction between the header and source files.

So, even though you implemented "jGroup::DoStuff ()" in a single file, that doesn't matter because it gets compiled into every including file. So, file_a.cpp gets compiled to file_a.obj, and file_b.cpp gets compiled to file_b.obj, and both contain a copy of jGroup::DoStuff (). The compiler is not smart, it will not check to see if they are identical and merge them.

The solutions are outlined in the article I linked you to, but essentially function definitions must either be in a source file (.cpp) or declared 'inline'.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this