各位请帮忙 看看这是何原因

shine51151 2005-03-21 11:45:01
friend Matrix & operator+ (const Matrix& ma1, const Matrix& ma2);

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
...全文
93 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
zengwujun 2005-03-23
  • 打赏
  • 举报
回复
学习
shine51151 2005-03-23
  • 打赏
  • 举报
回复
为什么我定义成员函数 Matrix & operator+ (const Matrix& ma2);就可以?
但定义友员函数 friend Matrix & operator+ (const Matrix& ma1, const Matrix& ma2);
就报错呢? 请高手解答一下好吗?
zhangfjj 2005-03-22
  • 打赏
  • 举报
回复
打错了吗?
那是友元函数哟!
yuchengliu 2005-03-22
  • 打赏
  • 举报
回复
MyComplex::MyComplex operator + (MyComplex& C1,MyComplex& C2)
{
double rre=C1.re+C2.re;
double rim=C1.im+C2.im;
MyComplex result(rre,rim);
return result;
}

楼上是不是打错了?还是?
MyComplex operator + (MyComplex& C1,MyComplex& C2)
{
double rre=C1.re+C2.re;
double rim=C1.im+C2.im;
MyComplex result(rre,rim);
return result;
}

zhangfjj 2005-03-22
  • 打赏
  • 举报
回复
以前回答的一个贴
#include <iostream>
using namespace std;
class MyComplex
{
double re,im;
public:
MyComplex():re(0),im(0){}
MyComplex(double m_re,double m_im)
{
re=m_re;
im=m_im;
}
friend MyComplex operator + (MyComplex& C1,MyComplex& C2);
void Display()
{
cout<<re<<"+"<<im<<"i"<<endl;
}
};
MyComplex::MyComplex operator + (MyComplex& C1,MyComplex& C2)
{
double rre=C1.re+C2.re;
double rim=C1.im+C2.im;
MyComplex result(rre,rim);
return result;
}
int main()
{
MyComplex a(2,3),b(3,4);
MyComplex c;
c=a+b;
cout<<"a=";
a.Display();
cout<<"b=";
b.Display();
cout<<"c=";
c.Display();
return 0;
}
【注意】
这段代码的调试,在dev-c++中是可以通过,但在vc6不行,老是出现
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 informationError executing cl.exe.
在网上查了一下资料,这里有一个前导声明的问题(限于友元方式重载运算符)
需要在类声明前面进行前导类的声明和友元函数的声明。
即:前面2行,大家在编使用vc编译器时要注意。
class MyComplex;
MyComplex operator + (MyComplex& C1,MyComplex& C2);
/*在使用友元重载运算符的时候,需要在类声明前面加上前导类的声明和函数的声明*/
class MyComplex
{
double re,im;
public:
MyComplex():re(0),im(0){}
MyComplex(double m_re,double m_im)
{
re=m_re;
im=m_im;
}
friend MyComplex operator + (MyComplex& C1,MyComplex& C2);
void Display()
{
cout<<re<<"+"<<im<<"i"<<endl;
}
};
MyComplex operator+ (MyComplex& C1,MyComplex& C2)
{
double rre=C1.re+C2.re;
double rim=C1.im+C2.im;
MyComplex result(rre,rim);
return result;
}
zhangfjj 2005-03-21
  • 打赏
  • 举报
回复
你没有把代码贴出来,怎么看呢?
估计是用vc6编译器。
vc6编译器在编译友元方式重载运算符时,有一个BUG
可能是吧
你试试在类的定义前面加这两行
class Matrix;//前导声明
Matrix & operator+ (const Matrix& ma1, const Matrix& ma2);//函数声明
class Matrix
{
//....类的定义
};


64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧