短短几行代码,欢迎近来坐坐。

yiruirui0507 2010-10-18 11:52:43
#include<iostream>
using namespace std;
class Point
{
int x,y;
public:
void set(int a,int b)
{
x=a;
y=b;
}
Point operator+(const Point &d)
{
Point s;
s.set(x+d.x,y+d.y);
return s;
}
friend ostream& operator<<(ostream& o,const Point& d);

};
inline ostream& operator<<(ostream& o,const Point& d)
{
return o<<"("<<d.x<<", "<<d.y<<")\n";
}
int main()
{
Point s,t;
s.set(2,5);
t.set(3,1);
cout<<s+t;
}
请问为什么编译出错呢,这里应该是友元函数才对呢。望大侠们指点。。。。
...全文
140 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
jikeyuan1 2010-10-18
  • 打赏
  • 举报
回复
vs2008 没有问题 <5,6>
vc6.0对输入输出表现出很多的不兼容 还是换了吧
yiruirui0507 2010-10-18
  • 打赏
  • 举报
回复
哦,感谢大家的意见。我用的还真是VC 6
赵文波 2010-10-18
  • 打赏
  • 举报
回复
我之前看错了= =

vs2008编译无问题。

VS6.0是 friend ostream& operator<<(ostream& o,const Point& d);
出的问题吧?
设一下断点吧。
现在一般没有公司在用vc6做开发平台了吧?楼主换VS2008吧
ayw215 2010-10-18
  • 打赏
  • 举报
回复
lz显然还在用vc6啊。。。扔了吧
乐CC 2010-10-18
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 baihacker 的回复:]
gcc 4.5.1表示毫无问题
vc6表示压力很大
[/Quote]
能否细说下?
gules 2010-10-18
  • 打赏
  • 举报
回复
friend ostream& operator<<(ostream& o,const Point& d);
改为:
ostream& print(ostream& os) const
{
os<<"("<<d.x<<", "<<d.y<<")\n";
}

operator<<函数中改为:
return d.print(o);
小楫轻舟 2010-10-18
  • 打赏
  • 举报
回复
VS2008 没问题
输出(5,6)
赵文波 2010-10-18
  • 打赏
  • 举报
回复
Point operator+(const Point &d)

你虽然重载了+号,但是返回的是Point类型 cout 能输出Point吗?返回整型就对了。
baihacker 2010-10-18
  • 打赏
  • 举报
回复
gcc 4.5.1表示毫无问题
vc6表示压力很大

65,186

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧