weixin_43140321 2018-09-27 03:39 采纳率: 0%
浏览 717

C++重载的运算符不能一起调用吗

#include

/* run this program using the console pauser or add your own getch, system("pause") or input loop /
using namespace std;
class Counter{
private:
int count;
public:
Counter(int i=0):count(i){}
Counter operator +(Counter&c)const{return Counter(count+c.count);}
Counter operator -(Counter&c)const{return Counter(count-c.count);}
Counter operator ++(){return Counter(++count);}
Counter operator ++(int){Counter c(*this);++(*this);return c;}
friend ostream&operator <<(ostream&out,Counter&c);
};
int main(int argc, char
* argv) {
Counter c1(1),c2(2),c3(3),temp;
cout<<c1<<endl;
temp=(c1+c2);
cout<<temp<<endl;
temp=(c3-c1);
cout<<temp<<endl;
temp=(c3++)+c3+c3;
cout<<(c3++)<<endl;//重载了<<和++,这里为什么不能直接调用呢
cout<<c3<<endl;
temp=(++c1);
cout<<temp<<endl;
return 0;
}
ostream&operator <<(ostream&out,Counter&c){
out<<"The value of the counter:"<<c.count;
return out;
};

  • 写回答

2条回答 默认 最新

  • xingjianfengaa 2018-09-27 07:15
    关注

    你的前++和后++估计都不行 因为<<的参数是Counter&c 而你的++返回值是Counter类型不匹配,你把++的返回值改为Counter&,再调整一下函数应该就只可以了

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog