Results 1 to 4 of 4

Thread: Using namespace enums in headers

  1. #1
    Join Date
    Jan 2011
    Posts
    70
    Thanks
    43
    Thanked 4 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Using namespace enums in headers

    Enum types are great in that they enforce certain values and protect functions from receiving values outside of the desired range. I'm trying to encapsulate my enums in a namespace so that they aren't on the global scope, but I'm hitting some issues that I think are related to circular dependencies. Here's some code excerpts.

    color_namespace.h
    Qt Code:
    1. #ifndef COLOR_NAMESPACE_H
    2. #define COLOR_NAMESPACE_H
    3.  
    4. #include "colorwriter.h"
    5. //#include lots of other headers
    6.  
    7. namespace CC
    8. {
    9. enum colors_t
    10. {
    11. BLACK = 0,
    12. RED = 1,
    13. BLUE = 2,
    14. GREEN = 4,
    15. WHITE = 8
    16. }
    17. }
    18. #endif
    To copy to clipboard, switch view to plain text mode 
    colorwriter.h
    Qt Code:
    1. #ifdef COLORWRITER_H
    2. #define COLORWRITER_H
    3.  
    4. #include "color_namespace.h"
    5.  
    6. class ColorWriter
    7. {
    8. public:
    9. ColorWriter();
    10. virtual bool writeColor(CC::colors_t c);
    11. }
    12. #endif
    To copy to clipboard, switch view to plain text mode 
    Now, the problem I'm running into is that I get a compiler error saying that 'CC' has not been declared. My research suggest there's a circular dependency, but how could that be when both headers have include guards? I would like to keep the #include in the namespace because there's a dozen other dependencies and I'd like to enable the user to only have to include one file instead of a dozen.

    If the enum was a class, I'd just forward declare it in the .h and #include the namespace in the .cpp, but I can't do that since enums can't be forward declared. Is the value protection provided by an enum simply not possible for functions declared outside that namespace?

    Thanks!

  2. #2
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Using namespace enums in headers

    Hi,

    Try adding this
    Qt Code:
    1. using namespace CC;
    To copy to clipboard, switch view to plain text mode 
    in colorwriter.h before the class definition.
    Òscar Llarch i Galán

  3. #3
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Using namespace enums in headers

    I would like to keep the #include in the namespace because there's a dozen other dependencies and I'd like to enable the user to only have to include one file instead of a dozen.
    Looks like in this case its enough to include "colorwriter.h" instead of "color_namespace.h".
    Just define the enum before any other class, it does not depend on them. My suggestion is to remove the #include "colorwriter.h" (and any other header that uses enum) from color_namespace.h, move the enum definition to separate file and include it in any source file depending on the enum, not the other way around.
    Simple fix could be to move the #include directives after enum definition.

  4. The following user says thank you to stampede for this useful post:

    Phlucious (30th November 2011)

  5. #4
    Join Date
    Jan 2011
    Posts
    70
    Thanks
    43
    Thanked 4 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Using namespace enums in headers

    Thanks stampede! Both suggestions have proven very useful.

    Moving the enum to be before the #defines worked so that I could simply #include the namespace from within any of the files relying on the enums defined within the namespace, while still allowing an end user to #include "color_namespace.h" and maintain full functionality of every feature.

    Separating the enum definition into another header to keep things clean also worked, but it meant that I had to #include multiple files instead of just one.

Similar Threads

  1. converting strings into enums
    By ugluk in forum Qt Programming
    Replies: 5
    Last Post: 26th August 2011, 10:59
  2. How to use enums in qml
    By nightroad in forum Qt Quick
    Replies: 0
    Last Post: 29th June 2011, 13:24
  3. Save enums in QSettings
    By redhat in forum Qt Programming
    Replies: 4
    Last Post: 22nd January 2009, 11:22
  4. Using enums in QtScript
    By Orphelic in forum Qt Programming
    Replies: 1
    Last Post: 29th November 2007, 11:55
  5. how to use enums
    By soul_rebel in forum General Programming
    Replies: 3
    Last Post: 23rd March 2006, 21:49

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.