我想重载>>运算符使用它像cin与我的类。下面是代码:不能重载>>运算符与istream C++
class Base {
public:
int mx;
Base() {}
Base(int x) : mx(x) {}
friend std::istream &operator>>(std::istream &, Base &);
friend std::ostream &operator<<(std::ostream &, const Base &);
};
std::istream &operator >>(std::istream &in, Base &object) {
in >> object.mx;
return in;
}
std::ostream &operator <<(std::ostream &out, const Base &object) {
out << object.mx;
return out;
}
int main() {
Base test();
std::cin >> test;
std::cout << test;
system("PAUSE");
return 0;
}
当我尝试编译我碰到一个“错误C2679:二进制‘>>’:没有运营商发现这需要基本型(__cdecl *)(无效)的”右手操作'(或没有可接受的转换)“ 和Intellisense表示没有运算符'>>'匹配这些操作数。
ostream版本似乎没有任何问题。
为什么?
@DouglasEdward没问题。我看到你编辑了Q,所以问题2)在Q中只是一个错字,而不是你的代码,1)是真正的问题?迟早会发生在任何人身上:) –