PDA

View Full Version : How to compile a project with two classes with same name in different namespaces?



bytelivre
27th June 2010, 06:33
Hi,

I have a project (QtCreator 1.3.1 / Qt 4.6.2) that doesn't compile when two classes in different namespaces have the same name. Note that all the compiled files goes to the same directory (debug/release) and this causes the conflict.

I would like to do one of two things:
1. Output compiled files in a hierarchy like namespace (core::math::classname output to core/math/classname)
2. Output compiled files in the debug/release directory with prefix (core::math::className output to core_math_classname)


Thanks!

squidge
27th June 2010, 08:04
Why not just name the source files namespace_class.ext ?

bytelivre
27th June 2010, 18:23
Yes fatjuicymole.
This is a solution and I'm using this by now, but when the project grows a little this becomes inelegant.

Example:
class: namespace::subnamespace::namespace_subnamespace_cl assname
path/header: namespace/subnamespace/namespace_subnamespace_classname.h
output: debug/namespace_subnamespace_classname.o (OK)
but, #include tags wiil become redundant and long: #include "namespace/subnamespace/namespace_subnamespace_classname.h" (NOT SO OK)

But, by sure it's a solution!
Thanks!

tbscope
27th June 2010, 18:33
this becomes inelegant.

If you don't mind my opinion. If you do, just don't read it ;-)

What you suggest is extremely ugly. I guess you are used to write code in Java?
I prefer to give all my classes a unique name and in their separate files. I do use sub directories to organise the code a little bit. But I have never had a use for namespaces, how complex my program might be. In fact, I think it confuses people when they need to read a lot of complex code.

Edit: I have looked at code like Open Office etc... Ohh my dear sweet deity, that is beyond insane, especially the directory structures.

SixDegrees
27th June 2010, 22:38
If you've sequestered your classes within different namespaces, you won't get any name collisions unless you've managed to do something wrong. The most common error is excessive or inappropriate use of the 'using' statement, which can strip off namespaces completely and undo the protection they offer. Please post the offending code and the error messages you're receiving.

Also, I agree with tbscope: it looks like you're trying to emulate Java, rather than use good coding practices. Excessive namespace depth is not necessary in C++ and only serves to make code harder to read. If you need to organize classes, do so with libraries. If you're getting name collisions within your own project, that's a design problem, not a namespace problem.

Vit Stepanek
28th June 2010, 13:44
Excessive namespace depth is not necessary in C++ and only serves to make code harder to read

I'd disagree with you, use of namespaces is absolutely clear in C++. Sometimes you just need to give the classes same names. And sometimes you use other's code and there's no chance to "rename" it for your use. And, also, if you deliver a code to somebody, they would like to keep a chance to give their classes any names.

graeme
28th June 2010, 17:43
Take a look at the subdirs template for qmake. That might help you out. However, as other have suggested don't over do the namespaces:)

npclaudiu
28th June 2010, 21:03
I think that the best solution for this problem is to have a corresponding directory for each namespace. That is, having two namespaces, nsx and nsx::nsy, and two classes nsx::c1 and nsx::nsy::c1, the resulting filesystem heirarchy will be:


nsx/
nsy/
c1.cpp
c1.cpp

This way the namespaces will be kept in sync with the directory structure.