PDA

View Full Version : Building classes with the same name under different namespaces



e79ene
5th May 2010, 20:51
Hello.

I've got a problem building application that contains 2 classes with the same name under different namespaces.
AFAIK this situation is legal for C++ classes. However I was not able to build this application with qmake (Qt 4.6.2) because it creates derived files for those classes in the same directory.
I.e.
1. object files (Foo.o)
2. moc files (moc_Foo.cpp)
3. uic generated files (ui_Foo.h)

As these files for both classes are generated in the same directory they override one another and the build fails.

Is there a solution for this problem? (Except avoiding duplicate class names)

Thanks.

squidge
5th May 2010, 21:14
I thought the output files were based on the filenames, not the class names.

e79ene
5th May 2010, 22:05
Yes.
And the source filenames are the same as classs name (Foo.h, Foo.cpp, Foo.ui). They are located in a different directories named after the namespaces.

norobro
5th May 2010, 22:40
I think FJM's point was that your file names don't have to be the class name. Try changing one set to anotherfoo.h etc.

e79ene
6th May 2010, 08:23
Thanks for suggestion.
Yes. Changing file names make the build pass.

However this doesn't look like a complete solution.
I meen that it seems natural when file name is the same as class name. Moreover this is the way how Qt Creator gives names to the source files.

I expect the build system to avoid file name conflicts by placing generated files in the directory structure similar to source files location. But I guess it's impossible with qmake. : (

squidge
6th May 2010, 08:40
I believe it is possible by editing the .pro file yourself, but it's far easier to let Qt Creator do it for you. For example, why not have the filename more like <namespace>_<classname>.cpp/.h ?

e79ene
6th May 2010, 09:01
I got your point.
Thanks.

axeljaeger
6th May 2010, 10:52
It depends on how you build your application. If you put your files in a directory structure that matches your namespace hierarchy, lets say you have two classes: a::c and b::c, and you put them in a/c.h ab b/c.h, then you cannot use them from the same .pro-file. However, you can create a .pro-file for each directory that is creating a static library and then link you binary against both of them.