Sign in to follow this  

namespace error

This topic is 3151 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

This topic is 3151 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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