【发布时间】:2018-06-11 15:41:15
【问题描述】:
我试图重载“
invalid initialization of non-const reference of type 'std::ostream&'
{aka 'std::basic_ostream<char>&' from an rvalue of type 'void'
return v.init();
这是我的类定义:
template<class T>
class Vector
{
private:
std::vector<T> _Vec;
public:
void fillVector();
void printElements();
void init() { fillVector(); printElements(); }
friend std::ostream& operator<<(std::ostream& os, Vector& v) {
return v.init();
};
我该如何解决?
【问题讨论】:
-
return os;,不是v.init();的结果(即void) -
_Vec是reserved identifier -
@PiotrSkotnicki 谢谢!这解决了我的问题。
-
我希望您还需要修改
printElements以使用os。
标签: c++ templates operator-overloading ostream