C++操作符重载问题,请高手帮忙!
钱能主编的<<C++程序设计教程(第二版)>>第262页有这样一个例子://**********************************************/...
钱能主编的<<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
{
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);"
函数定义之前又没有使用.不明白,请高手再说的详细一点. 展开
3个回答
展开全部
我也调试了一遍,找不到错误,我也很想知道哪错了,哪位高手说得详细一些吧...
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
是不是编译器有问题啊!用的vc++
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#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();
}
//########################################################
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();
}
//########################################################
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询