重載運算符為友元函數時出錯!!!! 急 在線等
可以重載為成員函數,但是當我重載為友元函數時就出錯.. 不知道哪們大蝦幫忙解釋一下.以下是出錯信息:
--------------------Configuration: 1 - Win32 Debug--------------------
Compiling...
1.cpp
E:\VC\VC6CN\COMMON\MSDEV98\BIN\1\1.cpp(10) : 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
执行 cl.exe 时出错.
1.exe - 1 error(s), 0 warning(s)
以下是程序:
#include<iostream>
using namespace std;
class Vector1
{
int x,y;
public:
Vector1() { };
Vector1(int x1,int y1) { x=x1; y=y1; }
friend Vector1 operator +(Vector1 v1,Vector1 v2) //提示這裡有錯..
{ Vector1 tmp;
tmp.x=v1.x+v2.x;
tmp.y=v1.y+v2.y;
return tmp;
}
void disp()
{ cout<<"("<<x<<","<<y<<")"<<endl;
}
};
void main()
{ Vector1 v1(6,8),v2(3,6),v3;
v3=v1+v2;
cout<<"v1+v2=";
v3.disp();
}
可以换个编译器
1.比如:DEV-C++;2.或把#include<iostream>
改成:#include <iotream.h>,同时把using namespace std删掉,程序中的 cout改为std::cout,而不用std名字空间;
3.vc6.0中同时用std与friend有冲突;
错了,
是#include <iostream.h>,而不是#include <iotream.h>刚打错了 顶2楼!这2个方法都是有效的。还有一个方法,LZ可以试试看。
VC++6对友元运算符重载的支持存在问题,如要使用名字空间,必须先在类外声明该友元函数。
详细代码可以看一下《易学C++》第十六章的例程,希望对LZ有帮助。[tk11]
呵呵,又学了一招!
刚试了一下,确实是,谢谢!回复 4# p1s 的帖子
还在吗?我也做过这个函数,我在函数形参那用的是引用,这样函数能顺利执行
页:
[1]
