【发布时间】:2021-04-26 17:06:53
【问题描述】:
我无法重载左移运算符“
Foo bar;
bar << 1 << 2 << 3;
我的班级 Foo 如下所示:
class Foo{
private:
vector<int> list;
public:
Foo();
void operator<<(int input);
};
还有这样的实现:
void Foo::operator<<(int input)
{
// here i want to add the different int values to the vector
// the implementation is not the problem
}
代码不起作用我得到一个错误“左操作数是'void'类型”。当我将返回类型更改为 Foo& 时,它会告诉我返回 Foo 类型的内容。问题是我做不到。我缺少对象 bar 的对象引用。
我搜索了很多,但只找到了描述要输出到 cout 的运算符的页面。
【问题讨论】:
-
你有没有把最后一行实现
Foo& operator<<() {... return *this;}?i -
@MatG 非常感谢!成功了!