| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1001 人关注过本帖
标题:操作符重载的问题
只看楼主 加入收藏
air8712
Rank: 1
等 级:新手上路
帖 子:19
专家分:0
注 册:2006-3-7
收藏
 问题点数:0 回复次数:6 
操作符重载的问题

#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.x);
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;

return 0;
}


VC下编译
C:\Documents and Settings\Administrator\桌面\b.cpp(17) : error C2248: 'x' : cannot access private member declared in class 'Point'
C:\Documents and Settings\Administrator\桌面\b.cpp(5) : see declaration of 'x'
C:\Documents and Settings\Administrator\桌面\b.cpp(17) : error C2146: syntax error : missing ';' before identifier 'd'
C:\Documents and Settings\Administrator\桌面\b.cpp(17) : error C2248: 'y' : cannot access private member declared in class 'Point'
C:\Documents and Settings\Administrator\桌面\b.cpp(5) : see declaration of 'y'
C:\Documents and Settings\Administrator\桌面\b.cpp(17) : error C2297: '<<' : illegal, right operand has type 'char [3]'
C:\Documents and Settings\Administrator\桌面\b.cpp(25) : error C2593: 'operator <<' is ambiguous
Error executing cl.exe.

b.obj - 5 error(s), 0 warning(s)

请问该怎么改

搜索更多相关主题的帖子: 操作符 重载 
2006-03-23 13:40
air8712
Rank: 1
等 级:新手上路
帖 子:19
专家分:0
注 册:2006-3-7
收藏
得分:0 
自己顶`~
2006-03-26 16:59
幽园香客
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:231
专家分:142
注 册:2006-2-27
收藏
得分:0 
这是在我自己机子调试成功。
#include <iostream>
using namespace std;

class Point
{


public:
int x;
int y;

friend ostream &operator<< (ostream &o, const Point &d );

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;
}


};

ostream &operator << (ostream & o, Point &d)
{
o << "(" << d.x << ", " <<d.y << ")\n";
return o;
}


int main()
{
Point s, t;
s.set (2, 5);
t.set (3, 1);
cout << s+t;

return 0;
}

做个有用的人!
2006-03-26 20:47
air8712
Rank: 1
等 级:新手上路
帖 子:19
专家分:0
注 册:2006-3-7
收藏
得分:0 
public:
int x;
int y;

这样就把数据改为共有了。。。
2006-03-27 10:24
柳儿
Rank: 6Rank: 6
等 级:贵宾
威 望:25
帖 子:1830
专家分:30
注 册:2004-9-23
收藏
得分:0 
以下是引用air8712在2006-3-23 13:40:00的发言:

#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.x);
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;

return 0;
}


VC下编译
C:\Documents and Settings\Administrator\桌面\b.cpp(17) : error C2248: 'x' : cannot access private member declared in class 'Point'
C:\Documents and Settings\Administrator\桌面\b.cpp(5) : see declaration of 'x'
C:\Documents and Settings\Administrator\桌面\b.cpp(17) : error C2146: syntax error : missing ';' before identifier 'd'
C:\Documents and Settings\Administrator\桌面\b.cpp(17) : error C2248: 'y' : cannot access private member declared in class 'Point'
C:\Documents and Settings\Administrator\桌面\b.cpp(5) : see declaration of 'y'
C:\Documents and Settings\Administrator\桌面\b.cpp(17) : error C2297: '<<' : illegal, right operand has type 'char [3]'
C:\Documents and Settings\Administrator\桌面\b.cpp(25) : error C2593: 'operator <<' is ambiguous
Error executing cl.exe.

b.obj - 5 error(s), 0 warning(s)

请问该怎么改

据说vc6.0都有这个问题,好像有补丁解决了。上网搜一下,很多相关资料。
我用borlanC++,不存在着中编译错误。


成功会使人骄傲。如果你骄傲自大,你就会停止学习。不学习,人就停止了进步
2006-03-27 14:21
幽园香客
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:231
专家分:142
注 册:2006-2-27
收藏
得分:0 
是的,再仔细看过。发现如果把 x、y改为共有,有点违背原意了!呵呵,其实,问题就是楼上说的,缺少一个符号。还是楼上的一针见血!

做个有用的人!
2006-03-27 15:39
air8712
Rank: 1
等 级:新手上路
帖 子:19
专家分:0
注 册:2006-3-7
收藏
得分:0 

明白了
虽然return o << "(" << d.x << ", " << d.y << ")\n";//少了<<
这里有问题

不过关键还是“据说vc6.0都有这个问题,好像有补丁解决了。上网搜一下,很多相关资料。我用borlanC++,不存在着中编译错误。”

我用DEV试了一下,OK,果然是VC6.0的问题。。。


多谢柳儿~~

2006-03-27 17:26
快速回复:操作符重载的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018088 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved