Two classes, same name - solution?

Started by
19 comments, last by Juliean 10 years, 11 months ago

Hello,

after refactoring some of my old gui code by removing the "Gui" - prefixes and instead putting them into a gui-namespace, plus putting the file into a seperate folder, I've come across a problem. Now I've got some classes that share the same name, and also the same file-name:


// in "Gui/Window.h"
namespace gui
{
class Window {};
}

// in "Core/Window.h"
namespace core
{
class Window {};
}


Those two classes doesn't have anything in common. Before starting, I supposed this was safe, since both files are in different locations, and both classes are in different namespaces. However, when compiling the libary I get a compiler warning about multiple object-files with the same name, and upon compiling the actual game project, I get a lot of unresolved symbol linker errors.

Now I ask, is there any way to solve this, without adding some provisorical pre/postfix to the class?

Advertisement

There shouldn't be any linker errors. Are you sure it's caused by this? Did you refactor your .cpp files as well, not only headers?

If both classes are still in separate namespaces then you can refer to them by prefixing them with the namespace:

Gui::Window
Core::Window
The whole point of namespaces is to allow what you are doing. Try to create a minimal program that shows the error. You'll probably discover the true problem in the process of writing that minimal program. And if you don't, you can post it here and we'll help you out.

Here you go. The errors I get are basically:


1>Debug\A.obj : warning LNK4042: Objekt mehrmals angegeben; zusätzliche Objekte werden ignoriert.
1>main.obj : error LNK2019: Verweis auf nicht aufgelöstes externes Symbol ""public: __thiscall a::A::A(void)" (??0A@a@@QAE@XZ)" in Funktion "_main".
1>main.obj : error LNK2019: Verweis auf nicht aufgelöstes externes Symbol ""public: __thiscall a::A::~A(void)" (??1A@a@@QAE@XZ)" in Funktion "_main".
1>S:\Users\Juli\Documents\Visual Studio 2012\Projects\TestProject\Debug\TestProject.exe : fatal error LNK1120: 2 nicht aufgelöste Externe
 


Sorry for the german, I hope you get what it means. LNK4042: translates to "object referenced multiple times", and the other ones to "unsolved refernece to extern symbol".

I know how I can reference the classes, but this errors are just... weird, or am I missing something? Using visual studio 2012 ultimate, if that helps.

This is indeed very weird. I got the same error bilding the project in MSVC++ Express 2012.

Renaming the files in B to B.h and B.cpp "fixed" it. Is this a compiler/linker bug?

It looks like you're not running into a C++ problem per se, but a problem with MSVC. By default, for every source file, MSVC creates an object file of the same name in Debug/ or Release/. This is still the case if you have two source files with the same name in two different directories. In this case you have A/A.cpp and B/A.cpp, both of which will try to use Debug/A.obj as the object file. One way to get around this problem is to rename the source files so there isn't a clash of object files. Another is to manually change the output file names for the source files in the solution explorer.

Please use a "normal" archiving format, like zip. Using something weird, like rar, will make people less likely to download the code.

It looks like you're running into MSVCs issue with files with the same name. At least that's my guess. It's technically a bug in MSVC. It's kind of a pain in the butt; I wish they'd fix it.

Edit: SiCrane... It is unholy how ninja-like you are!

Edit edit: @downvoter: care to expand on the reason behind the downvote? I'm not offended or anything, but I am puzzled about what deserved a downvote, and if there's something I can improve I'd like to know.

[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

Please use a "normal" archiving format, like zip. Using something weird, like rar, will make people less likely to download the code.

Wait, rar a weird archiving format? In all due honesty, thats a new one for me, rar has been my "standard" for years now. But, never mind, I strongly suppose you know what you are talking about, so, yeah, will relate to zip in the future.

Anyway, thanks both of you for pointing that out. I wasn't able to find a feasable solution in the solution explorer - while I could change the obj output directory for a project, that doesn't work for a single class - I'll have no choice than renaming it. I might as well split the modules into seperate projects somewhere, but right now, the complexity doesn't justify that. Man, thats really a pain, I'd too like them to fix that...

You don't need to change the output directory for the project, but you can change the generate object file name for a specific file. Go to the solution explorer, right click on the file name and hit properties, go to the output files page and change the output file name.

This topic is closed to new replies.

Advertisement