Hello all! I have exhausted all forms of searching for my answer here (I have googled, researched MSDN, searched this forum, and asked all my programming buddies if they know how to go about this) and tried many random implementations on my own but I am still having difficulties understanding what I'm supposed to do. I am developing this for use with both Microsoft Visual C++ .Net 2003, and the latest GNU compiler, so it will be cross-platform. (Windows and Linux)
I am constructing a serialized debug-output class. This debug class has 5 levels of severity: errors, warnings, information, debug, and spam. I would like to have the severity level controlled through the serialization operators using a static enum. Before I post example code I should mention this class follows the Singleton pattern and resides as a local object to the program as a reference, not a pointer. That said, this is an example of how I want this to work:
Code:
debugobject << Debug::error << "I am an error!" << endl;
debugobject << "I am still an error!" << endl;
debugobject << Debug::debug;
debugobject << "But now I'm debug!" << endl;
Here are the snippets of code relevant to my problem inside the class:
Code:
public:
static enum levels { error, warn, info, debug, spam } LEVELS;
static enum version { anyversion, version1, version2 } VERSIONS;
friend Debug& operator << (Debug &debug, Debug::levels lvl);
friend Debug& operator << (Debug &debug, Debug::version ver);
private:
int currentlevel;
int currentversion;
The all-capitals "LEVELS" and "VERSIONS" are required to conform to ISO C++ standards... at least according to GNU, though without those it compiles on MSVC with no problem. The above definition compiles on both MSVC and GNU.
This is the implementation of the operators:
Code:
Debug& operator << (Debug &debug, Debug::levels lvl) {
debug.currentlevel = lvl;
return debug;
}
Debug& operator << (Debug &debug, Debug::versions ver) {
debug.currentversion = ver;
return debug;
}
This also compiles correctly on both MSVC and GNU. However, trying to actually code a use for this creates 2 different errors on both GNU and MSVC:
Code:
debug << (DebugFile::debug) << "Debug!" << endl;
This is what MSVC says:
Code:
test-debug.cpp(21): error C2679: binary '<<' : no operator found which takes a
right-hand operand of type 'const char [16]' (or there is no acceptable conversion)
This is what G++ says:
Code:
test-debug.cpp:21: error: no match for 'operator<<' in 'operator<<((+debug),
DebugFile::debug) << "Debug!"'
DebugFile.h:41: error: candidates are: DebugFile& operator<<(DebugFile&,
DebugFile::levels)
DebugFile.h:42: error: DebugFile& operator<<(DebugFile&,
DebugFile::version)
I've been trying to get this figured out all day and I'm about ready to do something drastic like make an entire seperate class for this sort of thing. I do not wish to have the severity-level-setting parameters passed as strings or ints as I want this debug class to be "solid" and not prone to missing output because it was sent an integer of 1 or a special string to change the severity level of the output.
Thank you for your help in advance!