关于C++友元函数的重载操作符,为什么会报错
#include<iostream>usingnamespacestd;classA{inti;public:A(inta=0){i=a;}voidShow(void){...
#include<iostream>using namespace std;
class A
{ int i;
public:
A(int a=0) { i=a; }
void Show(void) { cout<<"i="<<i<<endl; }
friend A operator +(A &,A &);
};
A operator +(A &a , A &b) {A t; t.i=a.i+b.i; return t; }
void main(void)
{
A a1(10),a2(20),a3;
a1.Show ();
a2.Show ();
a3=a1+a2;
a3.Show ();
}
--------------------Configuration: class_test - Win32 Debug--------------------
Compiling...
class_test.cpp
C:\Documents and Settings\dell\My Documents\mystring\class_test.cpp(9) : 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.
class_test.obj - 1 error(s), 0 warning(s) 展开
class A
{ int i;
public:
A(int a=0) { i=a; }
void Show(void) { cout<<"i="<<i<<endl; }
friend A operator +(A &,A &);
};
A operator +(A &a , A &b) {A t; t.i=a.i+b.i; return t; }
void main(void)
{
A a1(10),a2(20),a3;
a1.Show ();
a2.Show ();
a3=a1+a2;
a3.Show ();
}
--------------------Configuration: class_test - Win32 Debug--------------------
Compiling...
class_test.cpp
C:\Documents and Settings\dell\My Documents\mystring\class_test.cpp(9) : 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.
class_test.obj - 1 error(s), 0 warning(s) 展开
2个回答
展开全部
从错误提示来看是编译器提示的错误,应该与代码无关。
The compiler cannot generate correct code for a construct, probably due to the combination of an expression and an optimization option. Try removing one or more optimization options and recompiling the function containing the line indicated in the error message.
------------从MSDN上查C1001得出的结论。
大致意思是编译器无法生成正确的代码,可能是你开了或改了一些优化选项。
具体的你自己再看看解释吧。
The compiler cannot generate correct code for a construct, probably due to the combination of an expression and an optimization option. Try removing one or more optimization options and recompiling the function containing the line indicated in the error message.
------------从MSDN上查C1001得出的结论。
大致意思是编译器无法生成正确的代码,可能是你开了或改了一些优化选项。
具体的你自己再看看解释吧。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include<iostream>
using namespace std;
class A
{ int i;
public:
A(int a=0) { i=a; }
void Show(void) { cout<<"i="<<i<<endl; }
friend A operator +(A &,A &);
};
A operator +(A &a , A &b) {A t; t.i=a.i+b.i; return t; }
int main()
{
A a1(10),a2(20),a3;
a1.Show ();
a2.Show ();
a3=a1+a2;
a3.Show ();
}
这样就好了,具体什么原因我也不太清楚。
using namespace std;
class A
{ int i;
public:
A(int a=0) { i=a; }
void Show(void) { cout<<"i="<<i<<endl; }
friend A operator +(A &,A &);
};
A operator +(A &a , A &b) {A t; t.i=a.i+b.i; return t; }
int main()
{
A a1(10),a2(20),a3;
a1.Show ();
a2.Show ();
a3=a1+a2;
a3.Show ();
}
这样就好了,具体什么原因我也不太清楚。
追问
还有问题,我这样改后#include不报错,不知为什么
#include
class A
{ int i;
public:
A(int a=0) { i=a; }
void Show(void) { cout<<"i="<<i<<endl; }
friend A operator +(A &,A &);
};
A operator +(A &a , A &b) {A t; t.i=a.i+b.i; return t; }
int main()
{ A a1(10),a2(20),a3;
a1.Show (); a2.Show ();
a3=a1+a2; a3.Show ();
return 0;}
追答
你用的编译器版本太老了吧
vc 6.0??? 建议你换一个,换个高版本的吧。我用的是C-FREE 5.0 还不错,而且也比较方便。试试吧。
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询