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

[求助]友元 重载+出错

hnzzc 发布于 2006-12-08 13:30, 758 次点击

#include <iostream>
using namespace std;
class Point
{
private:
int x;
int y;
public:
Point(){};
Point (int a,int b)
{
x=a;
y=b;
}
~Point(){};
void set(int a,int b);
int get_x(){return x;}
int get_y(){return y;}
void print() const
{
cout<<"("<<x<<","<<y<<")\n";
}
friend Point operator + ( Point &a,Point &b);
};

void Point::set(int a,int b)
{
x=a;
y=b;
}

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

void main()
{
Point a,b;
a.set(3,2);
b.set(1,5);
(a+b).print();
//add(a,b).print();

}

:\C++\c++作业\3\d.cpp(85) : 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 时出错.

请问:这个是什么意思??

8 回复
#2
hnzzc2006-12-08 15:35
已经发现错误了,
将#include <iostream>
using namespace std;
改成#include<iostream.h>
就可以了.
#include <iostream>
using namespace std;和#include<iostream.h>有什么区别
#3
zhanghuan_102006-12-08 16:12
main.cpp:32: error: expected constructor, destructor, or type conversion before "operator"
main.cpp:32: error: expected `,' or `;' before "operator"
:: === Build finished: 2 errors, 0 warnings ===
我用我的编译器编译之后还是有错误!具体如上!呵呵!不知道为什么!
#4
kai2006-12-09 12:13
你的程序可以是这样的, 使用friend function
[CODE]
#include <iostream>
using namespace std;
class Point
{
private:
int x;
int y;
public:
Point (int a = 0,int b = 0)
{
x=a;
y=b;
}
void set(int a,int b);
int get_x(){return x;}
int get_y(){return y;}
void print() const
{
cout<<"("<<x<<","<<y<<")\n";
}
friend Point operator + (const Point & a, const Point & b);
};

void Point::set(int a,int b)
{
x=a;
y=b;
}

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

int main()
{
Point a,b;
a.set(3,2);
b.set(1,5);

(a+b).print();
(a+b).print();
return 0;
}

[/CODE]

你的程序也可以是这样的, 使用member function
[CODE]
#include <iostream>
using namespace std;
class Point
{
private:
int x;
int y;
public:
Point (int a = 0,int b = 0)
{
x=a;
y=b;
}
void set(int a,int b);
int get_x(){return x;}
int get_y(){return y;}
void print() const
{
cout<<"("<<x<<","<<y<<")\n";
}
Point & operator + (const Point &a);
};

void Point::set(int a,int b)
{
x=a;
y=b;
}

Point & Point::operator + (const Point & a)
{
this->x += a.x;
this->y += a.y;
return *this;
}

int main()
{
Point a,b;
a.set(3,2);
b.set(1,5);

(a+b).print();
(a+b).print();
return 0;
}

[/CODE]

你可以比较一下这两者间的区别
#5
一二三四五2006-12-09 12:57

学习了

#6
maoguoqing2006-12-10 00:45

好像第一个程序还是会有那样的编译错误。。。

#7
kai2006-12-10 01:13
maoguoqing,

好像是很不严谨的一种说法。

如果程序在你的机器上运行时出错, 那么请你给出出错信息, 这样我们可以继续讨论下去。 同时也希望你能提供你所使用的编译器的名称。
#8
zhubenben2006-12-10 22:02
Point & Point::operator + (const Point & a)
{
this->x += a.x;
this->y += a.y;
return *this;
}

这个地方好像并不需要this->x
显示指出吧
Point & Point::operator + (const Point & a)
{
x += a.x;
y += a.y;
return *this;
}
这样就可以
类中每个成员函数都有个this指针
当对象调用这个函数时
this则指向那个对象
所以完全没必要写出
#9
maoguoqing2006-12-19 13:23
以下是引用kai在2006-12-10 1:13:40的发言:
maoguoqing,

好像是很不严谨的一种说法。

如果程序在你的机器上运行时出错, 那么请你给出出错信息, 这样我们可以继续讨论下去。 同时也希望你能提供你所使用的编译器的名称。

不好意思,我说好像其实是因为我也没有运行这个程序。。
我是用VC++6.0, 错误信息:
tal 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.
改为#include <iostream.h>这样引用头文件后便正确
但是我确实不明白是为什么,VS2003上很正确。。。

[此贴子已经被作者于2006-12-19 13:24:25编辑过]

1