myLogClass << "Line No: " << __LINE__ ...
你的operator <<
链接将无法正常工作,因为它返回了一个bool
.
bool myLogClass::operator << (const char * input)
通常按如下方式定义流插入:
std::ostream& myLogClass::operator << (std::ostream& o, const char * input) { // do something return o; }
做这个:
#define log(o, s) o << "Line No: " << __LINE__ << \ " Function: " << __FUNCTION__ << \ " Error: " << s // note I leave ; out
此外,您可以将宏包装在do-while
循环中:
#define log(o, s) do { o << "Line No: " << __LINE__ << \ " Function: " << __FUNCTION__ << \ " Error: " << s; \ } while(0) // here, I leave ; out
然后你可以高兴地写:
myLogClass myLogger; // do this // use it log(myLogger, "This is my error to be logged"); // note the ;