自动划片机:C++操作符重载问题,请高手帮忙!

来源:百度文库 编辑:杭州交通信息网 时间:2024/04/29 11:00:33
钱能主编的<<C++程序设计教程(第二版)>>第262页有这样一个例子:
//**********************************************
// 重载操作符
//**********************************************
#include <iostream>
using namespace std;
//##############################################
class Point
{
int x,y;
public:
void set(int a,int b)
{
x=a,y=b;
}
void print() const
{
cout<<"("<<x<<","<<y<<")\n";
}
friend Point operator+(const Point &a,const Point &b);
friend Point add( Point &a, Point &b);
};
//---------------------------------------------------------
Point operator+( Point &a,Point &b)
{
Point s;
s.set(a.x+b.x,a.y+b.y);
return s;
}//---------------------------------------------------------
Point add( Point &a, Point &b)
{
Point s;
s.set(a.x+b.x,a.y+b.y);
return s;
}//===========================================================
void main()
{
Point a,b;
a.set(3,2);
b.set(1,5);
(a+b).print();
operator+(a,b).print();
add(a,b).print();
}
//########################################################

出错信息如下:
--------------------Configuration: p262 - Win32 Debug--------------------
Compiling...
p262.cpp
C:\Documents and Settings\fhqfhq\p262.cpp(19) : 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.

p262.exe - 1 error(s), 0 warning(s)
请高手帮忙看看哪里有错?
为什么要加声明"Point operator+(Point &a,Point &b);"
函数定义之前又没有使用.不明白,请高手再说的详细一点.

#include <iostream>
using namespace std;
class Point;//要加声明
Point operator+(Point &a,Point &b);//要加声明
//##############################################
class Point
{
public:
int x,y;
void set(int a,int b)
{
x=a,y=b;
}
void print() const
{
cout<<"("<<x<<","<<y<<")\n";
}
friend Point operator+(const Point &a,const Point &b);
friend Point add( Point &a, Point &b);
};
//---------------------------------------------------------
Point operator+( Point &a,Point &b)
{
Point s;
s.set(a.x+b.x,a.y+b.y);
return s;
}//---------------------------------------------------------
Point add( Point &a, Point &b)
{
Point s;
s.set(a.x+b.x,a.y+b.y);
return s;
}//===========================================================
void main()
{
Point a,b;
a.set(3,2);
b.set(1,5);
(a+b).print();
operator+(a,b).print();
add(a,b).print();
}
//########################################################

我也调试了一遍,找不到错误,我也很想知道哪错了,哪位高手说得详细一些吧...

是不是编译器有问题啊!用的vc++