[英]why my compiler give error when i overload increment operator
I am facing a problem in operator overloading, when I compile my program it gives me an error, following is the code:我面临运算符重载的问题,当我编译我的程序时,它给了我一个错误,以下是代码:
class box
{
int length;
int width;
int volume;
public:
box():length(50),width(30)
{
}
box& operator++()
{
volume++;
width++;
length++;
}
};
int main()
{
box b1;
cout<<++b1;
}
The error it gives me is:它给我的错误是:
"no match for operator <<"
“不匹配运算符<<”
My question is why does the compiler give me an error although my syntax seems good?我的问题是为什么编译器给我一个错误,尽管我的语法看起来不错?
Two problems:两个问题:
Return *this from your overloaded operator.从您的重载运算符返回 *this。
If you want to print your object using "cout << ", you need to overload operator <<, otherwise print each member individually.如果要使用“cout <<”打印对象,则需要重载运算符 <<,否则单独打印每个成员。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.