Okay, I am mad. I am only mad because I've scoured the 'net, read all the books I own, and tried a million different combinations on my code. No avail. No one on the 'net has ever done this. None of the books think you can/should. And I certainly can't figure it out through combinations. So, even though I am certain I am missing something SSOOO simple, how do I do this:
Code:
/* Include nescessary headers */
#define ERROR CErrorHandler()
#define ERR_END CErrorHandler::end
class CErrorHandler
{
public:
typedef int end_type;
static end_type end;
template<typename T> CErrorHandler& operator<<(const T &Val)
{
Message << Val;
return (*this);
}
template<CErrorHandler::end_type> CErrorHandler& operator<<(const CErrorHandler::end_type &Val)
{
Message << "\n";
cout << "Error: " << this->str();
throw Message;
}
string str()
{
return Message.str();
}
private:
stringstream Message;
};
void main()
{
ERROR << "This is an error message" << ERR_END;
}
The above code doesn't work. As you can see, I want to make an error handler that works like a stream, and as soon as end_type type is collected in the stream, it will actually THROW the error. However, this doesn't work. I've tried everything, if I do a:
Code:
template<typename T> CErrorHandler& operator<<(const T &Val)
{
Message << Val;
return (*this);
}
template<> CErrorHandler& operator<< <CErrorHandler::end_type>(const CErrorHandler::end_type &Val)
{
Message << "\n";
cout << "Error: " << this->str();
throw Message;
}
Then g++ gives the following errors (that, as usual, are extremely descriptive... right... NOT!):
Code:
In file included from source/main.cpp:5:
include/main.h:27: error: invalid explicit specialization before '>' token
include/main.h:27: error: explicit specialization in non-namespace scope `class CErrorHandler'
include/main.h:28: error: invalid member function declaration
make: *** [source/main.o] Error 1