鱼C论坛

 找回密码
 立即注册
查看: 1283|回复: 5

[已解决]重载运算符参数加const

[复制链接]
发表于 2020-3-29 14:33:56 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
/*求复数相加*/
#include<iostream>
using namespace std;
class Complex
{
        public:
                Complex()
                {
                        real=0;
                        imag=0;
                }
                Complex(double t,double d)
                {
                        real=t;
                        imag=d;
                }
               
                Complex operator +(const int &x);         /*不加const显示编译错误*/
       
                void display()
                {
                        cout<<"("<<real<<","<<imag<<"!)"<<endl;
                }
        private:
                double real;
                double imag;
};
Complex Complex::operator +(const int &x)              /*不加const显示编译错误*/
{
        return Complex(real+x,imag);
}
int main()
{
        Complex t1(3.4,5.2);
        Complex t3,t4;
        t3=t1+2;
        t3.display();
        return 0;
}
不加const编译通不过,这是为什么呢,我也不需要改变int的值呀
最佳答案
2020-3-29 14:44:13
  1. #include <iostream>

  2. int main()
  3. {
  4.         int a = 1234;
  5.         int &r1 = a;
  6.         //int &r2 = 1234;        // 这个就不行
  7.         const int &r3 = 1234;        // 没问题
  8.         return 0;
  9. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-3-29 14:36:45 | 显示全部楼层
不应该的,看你编译器,你的编译器错误信息是什么
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-29 14:41:38 | 显示全部楼层
  1. #include<iostream>
  2. using namespace std;
  3. class Complex
  4. {
  5.         public:
  6.                 Complex()
  7.                 {
  8.                         real=0;
  9.                         imag=0;
  10.                 }
  11.                 Complex(double t,double d)
  12.                 {
  13.                         real=t;
  14.                         imag=d;
  15.                 }
  16.                
  17.                 Complex operator +(int &x);         /*不加const显示编译错误*/
  18.         
  19.                 void display()
  20.                 {
  21.                         cout<<"("<<real<<","<<imag<<"!)"<<endl;
  22.                 }
  23.         private:
  24.                 double real;
  25.                 double imag;
  26. };
  27. Complex Complex::operator +(int &x)              /*不加const显示编译错误*/
  28. {
  29.         return Complex(real+x,imag);
  30. }
  31. int main()
  32. {
  33.         Complex t1(3.4,5.2);
  34.         Complex t3,t4;
  35.         //t3=t1+2;                // 是这里导致的,加const就是为了这里
  36.         int a = 1234;
  37.         t3 = t1 + a;
  38.         t3.display();
  39.         return 0;
  40. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-3-29 14:42:54 | 显示全部楼层
time1970 发表于 2020-3-29 14:36
不应该的,看你编译器,你的编译器错误信息是什么

c.cpp(35) : error C2679: binary '+' : no operator defined which takes a right-hand operand of type 'const int' (or there is no acceptable conversion)
执行 cl.exe 时出错.
声明和下面定义都不加const会显示如上错误
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-29 14:44:13 | 显示全部楼层    本楼为最佳答案   
  1. #include <iostream>

  2. int main()
  3. {
  4.         int a = 1234;
  5.         int &r1 = a;
  6.         //int &r2 = 1234;        // 这个就不行
  7.         const int &r3 = 1234;        // 没问题
  8.         return 0;
  9. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-3-29 14:57:03 | 显示全部楼层
大河之jian 发表于 2020-3-29 14:42
c.cpp(35) : error C2679: binary '+' : no operator defined which takes a right-hand operand of type ...

楼下正解,引用只能对左值,常引用才可以引用右值
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-6 05:31

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表