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
#ifndef COLOR_NAMESPACE_H
#define COLOR_NAMESPACE_H
#include "colorwriter.h"
//#include lots of other headers
namespace CC
{
enum colors_t
{
BLACK = 0,
RED = 1,
BLUE = 2,
GREEN = 4,
WHITE = 8
}
}
#endif
#ifndef COLOR_NAMESPACE_H
#define COLOR_NAMESPACE_H
#include "colorwriter.h"
//#include lots of other headers
namespace CC
{
enum colors_t
{
BLACK = 0,
RED = 1,
BLUE = 2,
GREEN = 4,
WHITE = 8
}
}
#endif
To copy to clipboard, switch view to plain text mode
colorwriter.h
#ifdef COLORWRITER_H
#define COLORWRITER_H
#include "color_namespace.h"
class ColorWriter
{
public:
ColorWriter();
virtual bool writeColor(CC::colors_t c);
}
#endif
#ifdef COLORWRITER_H
#define COLORWRITER_H
#include "color_namespace.h"
class ColorWriter
{
public:
ColorWriter();
virtual bool writeColor(CC::colors_t c);
}
#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!
Bookmarks