注册 登录
编程论坛 C++教室

运算符重载问题,看不懂错误提示

唐兵 发布于 2013-06-18 16:43, 1360 次点击
#include <iostream>
using namespace std;
class Complex
{public:
friend Complex operator + (Complex &c1,Complex &c2);
Complex(double r=0,double i=0){real=r;imag=i;}
double get_real();
double get_imag();
void display();
double real;
double imag;
};
double Complex::get_real()
{return real;}
double Complex::get_imag()
{return imag;}

Complex operator + (Complex &c1,Complex &c2)
{Complex c;
 c3.real=c1.get_real()+c2.get_real();
 c3.imag=c1.get_imag()+c2.get_imag();
 return c ;
}
int main()
{Complex c1(3,4),c2(5,-10),c3;
c3=c1+c2;
cout<<"c3=";
cout<<c3;
return 0;
}
错误:fatal error C1001: INTERNAL COMPILER ERROR
        (compiler file 'msc1.cpp', line 1786)
         Please choose the Technical Support command on the Visual C++
         Help menu, or open the Technical Support help file for more information
Error executing cl.exe.
13 回复
#2
rjsp2013-06-19 08:14
编译器本身出错了
类似于阅卷老师突然病亡
#3
love云彩2013-06-19 11:34
#include <iostream>
using namespace std;
class Complex
{public:
friend Complex operator + (Complex &c1,Complex &c2);
Complex(double r=0,double i=0){real=r;imag=i;}
double get_real();
double get_imag();
void display();
double real;
double imag;
};
double Complex::get_real()
{return real;}
double Complex::get_imag()
{return imag;}
//定义display输出函数
void Complex::display()
{cout<<"("<<real<<","<<imag<<")"<<endl;}
Complex operator + (Complex &c1,Complex &c2)
{Complex c3;
c3.real=c1.get_real()+c2.get_real();
c3.imag=c1.get_imag()+c2.get_imag();
return c3 ;
}
int main()
{Complex c1(3,4),c2(5,-10),c3;
c3=c1+c2;
cout<<"c3=";
c3.display();//调用display函数输出信息
return 0;
}
只是修改了几处加注释的地方,然后就实现了楼主你想要的功能
#4
唐兵2013-06-19 16:10
回复 3楼 love云彩
我也看了书上可以同函数输出c3,但也可以直接输出啊。而且那段代码还是需要把friend Complex operator + (Complex &c1,Complex &c2);这段删除才能正确运行,咋的呢
#5
唐兵2013-06-19 16:11
回复 2楼 rjsp
但把friend Complex operator + (Complex &c1,Complex &c2);删除了就不是那个错误了
#6
子楠2013-06-19 17:23
求解
#7
子楠2013-06-19 17:31
这问题,晕,哪里出错了
#8
love云彩2013-06-20 02:04
回复 4楼 唐兵
等我明天中午考完试,下午再解释给你听,问题不大的
#9
唐兵2013-06-20 12:31
回复 8楼 love云彩
好的,谢谢
#10
唐兵2013-06-21 10:24
回复 8楼 love云彩
有空了没
#11
love云彩2013-06-21 13:55
回复 10楼 唐兵
#include <iostream>
using namespace std;
class Complex
{public:
friend Complex operator + (Complex &c1,Complex &c2);
Complex(double r=0,double i=0){real=r;imag=i;}
double get_real();
double get_imag();
void display();
double real;
double imag;
};
double Complex::get_real()
{return real;}
double Complex::get_imag()
{return imag;}

Complex operator + (Complex &c1,Complex &c2)
{Complex c;
c3.real=c1.get_real()+c2.get_real();
c3.imag=c1.get_imag()+c2.get_imag();
return c ;                         //红色标记的地方出错,对象不一致,你定义了一个临时对象为c,而你表达式里面却用了c3,返回值又用c,这是粗心,直接把c换成c3
}

int main()
{Complex c1(3,4),c2(5,-10),c3;
c3=c1+c2;
cout<<"c3=";
cout<<c3;                        //我不知道楼主你那些代码错误提示是什么意思,我只知道你没有重载输出流,所以不能使用cout<<来直接输出一个对象的数据
return 0;
}
要想直接输出(即cout<<c3),可以在类体内声明一个运算符重载函数,代码如下(包含你之前代码的某些地方修改):
#include <iostream>
 using namespace std;
 class Complex
 {public:
 friend ostream &operator<<(ostream &,const Complex &);  //声明一个友元运算符重载函数,重载"<<",这样就能实现直接使用cout输出一个对象的数据了
 friend Complex operator + (Complex &c1,Complex &c2);
 Complex(double r=0,double i=0){real=r;imag=i;}
 double get_real();
 double get_imag();
 void display();
 double real;
 double imag;
 };
double Complex::get_real()
 {return real;}
 double Complex::get_imag()
 {return imag;}
 ostream &operator<<(ostream &output,const Complex &p)   //定义运算符重载函数
 {
     cout<<"(";
     output<<p.real;
     cout<<",";
     output<<p.imag;
     cout<<")";
     return output;
 }
Complex operator + (Complex &c1,Complex &c2)
 {Complex c3;                              //这两个地方修改了
 c3.real=c1.get_real()+c2.get_real();
 c3.imag=c1.get_imag()+c2.get_imag();
return c3 ;
 }
 int main()
 {Complex c1(3,4),c2(5,-10),c3;
 c3=c1+c2;
 cout<<"c4=";
cout<<c3;                //只有添加一个重载“<<”的友元函数,才能实现楼主所说的直接输出一个对象的数据
 return 0;
 }
楼主不妨复制以上的代码来运行一次
#12
唐兵2013-06-23 13:58
非常感谢你的详细讲解,懂了很多。不过还是有那个问题,应该是编译器的问题,谢了哈
#13
love云彩2013-06-23 15:42
回复 12楼 唐兵
不太清楚楼主所说的那问题,如果是编译器的问题,那不必追究下去了,弄懂这个程序的思想就可以了
#14
唐兵2013-06-25 14:57
回复 13楼 love云彩
嗯,好
1