帮忙看看错在哪里???????

hb0800092 2009-04-09 02:15:50

#include<iostream>
using namespace std;
class complex
{
double real;
double imag;
public:
complex(double real_value,double imag_value)
{
real = real_value;
imag = imag_value;
}
friend complex operator +(complex c1, complex c2);
void display()
{
cout<<"("<<real<<"+"<<imag<<"i)"<<endl;
}
};
complex operator +(complex c1,complex c2)
{
complex c_result(0,0);
c_result.real = c1.real + c2.real;
c_result.imag = c1.imag + c2.imag;
return c_result;
}
int main()
{
complex c1(3,4);
complex c2(2,5);
complex c_result = c1+c2;
c_result.display();
return 0;
}


出现的错误是:
--------------------Configuration: complex - Win32 Debug--------------------
Compiling...
program.cpp
D:\Program Files\Microsoft Visual Studio\MyProjects\complex\program.cpp(14) : 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.

program.obj - 1 error(s), 0 warning(s)
我不会改 我也没遇到过这个错误 高手帮忙高下
...全文
230 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
breezehou 2009-04-11
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 hb0800092 的回复:]
引用 11 楼 stormlk1983 的回复:
应该是编译器的问题 ,另外LZ这样写
complex operator +(complex c1,complex c2)
{
complex c_result(0,0);
c_result.real = c1.real + c2.real;
c_result.imag = c1.imag + c2.imag;
return c_result;
}
返回一个局部对象是很危险的事情

可以说说 怎么危险吗 我是新手 麻烦 将下好不
[/Quote]
我觉得这里没太大问题,因为complex里的成员变量都是基本类型double,如果有指针型的成员变量,会是比较危险的。
Kevin_Perkins 2009-04-11
  • 打赏
  • 举报
回复

#include<iostream>

using namespace std;

class complex
{
public:
double real;
double imag;
public:
complex();
complex(double real_value,double imag_value)
{
real = real_value;
imag = imag_value;
}
complex(complex &complexIn);
complex operator + (complex c);
void display()
{
cout<<"("<<real<<"+"<<imag<<"i)"<<endl;
}
};

complex::complex()
{
real = 0;
imag = 0;
}

complex::complex(complex &complexIn)
{
this->real = complexIn.real;
this->imag = complexIn.imag;
}

complex complex::operator + (complex c)
{
complex tmpResult(0, 0);
tmpResult.real = this->real + c.real;
tmpResult.imag = this->imag + c.imag;
return tmpResult;
}

int main()
{
complex c1(3, 4);
complex c2(2, 5);
complex c_result = c1 + c2;
c_result.display();
return 0;
}

友员的问题,我改写了,没有用到友员,也许与你的初衷不同,仅供参考吧。
Jarrys 2009-04-10
  • 打赏
  • 举报
回复
VC 6.0只支持把友元函数放在类声明中。。。
hb0800092 2009-04-10
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 stormlk1983 的回复:]
应该是编译器的问题 ,另外LZ这样写
complex operator +(complex c1,complex c2)
{
complex c_result(0,0);
c_result.real = c1.real + c2.real;
c_result.imag = c1.imag + c2.imag;
return c_result;
}
返回一个局部对象是很危险的事情
[/Quote]
可以说说 怎么危险吗 我是新手 麻烦 将下好不
Hayden_yang 2009-04-09
  • 打赏
  • 举报
回复
gcc比较标准。。
zhous001 2009-04-09
  • 打赏
  • 举报
回复
VC6

friend complex operator +(complex c1, complex c2);
改成 friend complex operator +(complex c1, complex c2;
编译通过!
gaisifengbao 2009-04-09
  • 打赏
  • 举报
回复
顶!
Xiaowuasa 2009-04-09
  • 打赏
  • 举报
回复
vc6这么差劲?………………无奈了
zgjxwl 2009-04-09
  • 打赏
  • 举报
回复
VC6.0对友员本身支持就是不好。还有。。。比如说:在VC6.0中。。友员不能访问类的私有成员。
maosher 2009-04-09
  • 打赏
  • 举报
回复
VC2003编译通过,换个编译器吧
  • 打赏
  • 举报
回复

class complex
{
double real;
double imag;
public:
complex(double real_value,double imag_value)
{
real = real_value;
imag = imag_value;
}
friend complex operator +(complex c1, complex c2)   //是Vc6的话,友员的声明跟定义都放在类体里
{
complex c_result(0,0);
c_result.real = c1.real + c2.real;
c_result.imag = c1.imag + c2.imag;
return c_result;
}
void display()
{
cout<<"("<<real<<"+"<<imag<<"i)"<<endl;
}
};

int main()
{
complex c1(3,4);
complex c2(2,5);
complex c_result = c1+c2;
c_result.display();
return 0;
}


夹心饼干 2009-04-09
  • 打赏
  • 举报
回复
应该是编译器的问题 ,另外LZ这样写
complex operator +(complex c1,complex c2)
{
complex c_result(0,0);
c_result.real = c1.real + c2.real;
c_result.imag = c1.imag + c2.imag;
return c_result;
}
返回一个局部对象是很危险的事情
sjj_2412601003 2009-04-09
  • 打赏
  • 举报
回复
1楼正解,VC6.0对运算符重载的友元支持不够好,可以把complex operator +(complex c1, complex c2)修改为complex operator +(complex c1)当作类成员函数来处理
ylywyn136 2009-04-09
  • 打赏
  • 举报
回复
恩,换个编译器 试试吧
xiaocha 2009-04-09
  • 打赏
  • 举报
回复
学 C++,一定不要用VC6
yangch_nhcmo 2009-04-09
  • 打赏
  • 举报
回复
GCC 4.3.2下编译通过,可能与VC6有关吧,楼主可以换个编译器试试,VC6对标准支持不好
bitxinhai 2009-04-09
  • 打赏
  • 举报
回复
#include<iostream>
using namespace std;

改为#include<iostream.h>试试!!!!
feng4206yu 2009-04-09
  • 打赏
  • 举报
回复
把友元的函数内容放入类中实现,不要单独拿出来....
xqls_xqls 2009-04-09
  • 打赏
  • 举报
回复
#include "stdafx.h"
#include<iostream>
using namespace std;
class complex
{
double real;
double imag;
public:
complex(double real_value,double imag_value)
{
real = real_value;
imag = imag_value;
}
friend complex operator +(complex c1, complex c2)
{
complex c_result(0,0);
c_result.real = c1.real + c2.real;
c_result.imag = c1.imag + c2.imag;
return c_result;
}
void display()
{
cout<<"("<<real<<"+"<<imag<<"i)"<<endl;
}
};

int main()
{
complex c1(3,4);
complex c2(2,5);
complex c_result = c1+c2;
c_result.display();
return 0;
}

改为这样试试.
ilwmin 2009-04-09
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 tangshuiling 的回复:]
C/C++ code
楼主用的是vc6.0对友元支持不好,vs2005正常运行,如果非要用vc6.0,友元在类中实现
[/Quote]
楼上正解
VC6.0不是标准的C++编译器.
加载更多回复(2)

65,210

社区成员

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

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