【发布时间】:2018-10-05 09:04:39
【问题描述】:
有一个函数将 std::ostream& 作为参数并执行一些操作:
inline std::ostream& my_function(std::ostream& os, int n) {
// some operations
return os;
}
还有另一个函数调用my_function:
void caller_function(int n) {
std::ostringstream ostsr;
ostsr << my_function(ostsr, n);
}
visual studio 2015编译器报错:
error C2679: binary '<<' : no operator found which takes a left-hand operand of type 'std::basic_ostream<char, std::char_traits<char>>'
std::ostringstreamm 有一个继承和重载的operator<<,它在这种情况下采用操纵器函数,操纵器函数是my_function
重载运算符
ostream& operator<< (ostream& (*pf)(ostream&));
那么问题是什么以及如何解决?
【问题讨论】:
-
my_function(ostsr, n)不是操纵器函数。这是一个流引用。my_function是一个函数但是签名错误。
标签: c++ compiler-errors operator-overloading ostream ostringstream