Results 1 to 8 of 8

Thread: How to compile a project with two classes with same name in different namespaces?

  1. #1
    Join Date
    Jun 2010
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Question How to compile a project with two classes with same name in different namespaces?

    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!

  2. #2
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to compile a project with two classes with same name in different namespaces?

    Why not just name the source files namespace_class.ext ?

  3. #3
    Join Date
    Jun 2010
    Posts
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to compile a project with two classes with same name in different namespaces?

    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!

  4. #4
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: How to compile a project with two classes with same name in different namespaces?

    Quote Originally Posted by bytelivre View Post
    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.

  5. #5
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: How to compile a project with two classes with same name in different namespaces?

    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.

  6. #6
    Join Date
    Apr 2010
    Posts
    34
    Thanks
    1
    Thanked 5 Times in 5 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to compile a project with two classes with same name in different namespaces?

    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.

  7. #7
    Join Date
    Jan 2006
    Posts
    40
    Thanks
    3
    Thanked 2 Times in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to compile a project with two classes with same name in different namespaces?

    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

  8. #8
    Join Date
    Mar 2010
    Posts
    13
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: How to compile a project with two classes with same name in different namespaces?

    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:
    Qt Code:
    1. nsx/
    2. nsy/
    3. c1.cpp
    4. c1.cpp
    To copy to clipboard, switch view to plain text mode 
    This way the namespaces will be kept in sync with the directory structure.
    Come to the dark side. We have cookies.

Similar Threads

  1. Project suddenly doesn't compile
    By vieraci in forum Qt Programming
    Replies: 16
    Last Post: 7th June 2016, 07:12
  2. help,who could help me to compile qt project
    By derekli in forum Qt Programming
    Replies: 3
    Last Post: 13th May 2014, 15:27
  3. Replies: 7
    Last Post: 6th May 2010, 09:52
  4. Project compile problems
    By MarkoSan in forum Qt Programming
    Replies: 7
    Last Post: 15th October 2009, 12:54
  5. qmake: add to project but do not compile
    By jesse in forum Newbie
    Replies: 2
    Last Post: 8th November 2007, 16:03

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.