我想了解运营商重载如何工作。运营商的解释<< overload
我想代码,以便我可以写
Log(Log::LEVEL_ERR) << "fatal error: " << 13 ;
而对于字符串和重载运算符使用数量两者。
我现在有
class Log{
public:
std::ostream& operator<<(char const*);
}
std::ostream& Log::operator<<(char const* text){
if (Log::isToWrite()) {
printLevel();
std::cout << text;
}
return std::cout;
}
这只能得到是我的字符串,但数量不限,为什么呢?
编辑 @bitmask只是要清楚,你的意思是实现这样的:
class Log{
public:
friend Log& operator<<(Log& in, char const* text);
}
friend Log& operator<<(Log& in, char const* text){
if (in.isToWrite()) {
in.printLevel();
std::cout << text;
}
return std::cout;
}
因为我得到这些,现在到处:
error: Semantic Issue: Invalid operands to binary expression ('Log' and 'const char [15]')
也许这是非常简单的,但你能拼出来给我?
我真的不明白。
,因为它需要一个char *,和一个int不是隐式可转化为char * – 2012-04-02 00:24:45