注册 登录
编程论坛 C++教室

声明类函数operator+时,编译出错

ddsxd 发布于 2007-03-29 20:44, 587 次点击
程序全文如下,编译时出错的行已经改成红色,错误描述为: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
这是怎么回事儿啊???
ps:编译器是VC6.0


#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(const Point& a,const Point& b);
}
Point operator+(const Point& a,const Point& b)
{
Point s;
s.set(a.x+b.x,a.y+b.y);
return s;
}
Point add(const Point& a,const 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();
}

[此贴子已经被作者于2007-3-29 21:14:13编辑过]

2 回复
#2
游乐园2007-03-29 21:34

注意重载的+的使用方法, 给你改好了

程序代码:

#include <iostream>
using namespace std;


class Point;
Point operator +(const Point& a,const Point& b);
Point add(const Point& a,const Point& b); //VC++6 没有sp6补丁的必须有提前声明


class Point
{
   
friend Point operator +(const Point& a,const Point& b);
    friend Point add(const Point& a,const Point& b);


public:
    void set(int a,int b)
    {
        x=a,y=b;
    }
    void print()const
    {
        cout<<\"(\"<<x<<\",\"<<y<<\")\"<<endl;// 注意格式
    }
private:
     int x,y;

};
Point operator+(const Point& a,const Point& b)
{
    Point s;
    s.set(a.x+b.x,a.y+b.y);
    return s;
}


Point add(const Point& a,const 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();//+号的使用
    add(a,b).print();//add方法的使用
}

#3
ddsxd2007-04-02 12:32
谢谢!
帮了大忙了
1