【发布时间】:2015-09-01 21:52:46
【问题描述】:
我编写了自己的记录器,可以按如下方式使用:
LOG("This is the number five: " << 5 << ".");
我想写一个错误检查方法,它只接受一个返回码,检查它是否是一个错误,如果是则打印一个错误消息
因此,我想转:
if(result == -1)
{
LOG("ERROR! We got a result of: " << result << ".");
// Do some other stuff
}
进入:
checkForError(result, "ERROR! We got a result of: " << result << ".");
void checkForError(int result, SomeStringType message)
{
if(result == -1)
{
LOG(message);
// Do some other stuff
}
}
这可能吗?我尝试将它作为 char*、std:string 和 stringstream 传递,但我似乎无法让它正常工作
有什么方法可以做到这一点,所以我只是在函数调用中连接错误消息?
【问题讨论】:
-
你考虑过
ostringstream吗? -
#define LOG_ON_FAIL(result, prompt) if(-1 == result) {LOG((prompt)
-
字符文字永远不会很好地接受
"ERROR! We got a result of: " << result << "."(operator<<) 操作。这不是你得到这个工作的方式。
标签: c++ string concatenation