| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1703 人关注过本帖
标题:运算符重载问题
只看楼主 加入收藏
三少爷
Rank: 1
等 级:新手上路
帖 子:192
专家分:0
注 册:2004-4-29
收藏
得分:0 
以下是引用kai在2004-12-28 00:18:36的发言:

#include<iostream> #include<cstdlib> #include<cmath> using namespace std;

class point { private: double x,y; public: point(double xx=0,double yy=0){x=xx;y=yy;} friend void triangle(point &,point &,point &); friend istream &operator>>(istream &,point &); };

void triangle(point &p1,point &p2,point &p3) { double k=sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y)); double l=sqrt((p1.x-p3.x)*(p1.x-p3.x)+(p1.y-p3.y)*(p1.y-p3.y)); double s=sqrt((p3.x-p2.x)*(p3.x-p2.x)+(p3.y-p2.y)*(p3.y-p2.y)); if(k+l>s && k-l<s) { double m=(k*k+l*l-s*s)/(2*k*l); //ÕâÀïÓ¦¸Ã×¢Òâһϣ¬Ò»¶¨ÒªÀ¨ºÅ cout<<"Äܹ¹³ÉÈý½ÇÐΣ¬Ãæ»ýΪ£º"<<0.5*k*l*sqrt(1-m*m)<<endl; } else cout<<"²»Äܹ¹³ÉÈý½ÇÐÎ"<<endl; } istream &operator>>(istream &input,point &p) { input>>p.x>>p.y; // here is an error, instead of cin, you should set the parameter input as istream object return input; }

int main() { point p1,p2,p3; cout<<"ÇëÊäÈëÈý¸öµã£º"; cin>>p1>>p2>>p3; triangle(p1,p2,p3); system("pause");

return 0; }

aa.cpp C:\Windows\Desktop\aa.cpp(31) : error C2255: '>>' : a friend function can only be declared in a class C:\Windows\Desktop\aa.cpp(32) : error C2248: 'x' : cannot access private member declared in class 'point' C:\Windows\Desktop\aa.cpp(9) : see declaration of 'x' C:\Windows\Desktop\aa.cpp(32) : error C2248: 'y' : cannot access private member declared in class 'point' C:\Windows\Desktop\aa.cpp(9) : see declaration of 'y' C:\Windows\Desktop\aa.cpp(33) : error C2562: '>>' : 'void' function returning a value C:\Windows\Desktop\aa.cpp(30) : see declaration of '>>' C:\Windows\Desktop\aa.cpp(40) : error C2593: 'operator >>' is ambiguous Error executing cl.exe.

aa.exe - 5 error(s), 0 warning(s) 好像没用,不能访问私有成员

[此贴子已经被作者于2004-12-28 13:09:48编辑过]


2004-12-28 13:08
kai
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:52
帖 子:3450
专家分:59
注 册:2004-4-25
收藏
得分:0 
三少爷, 如果你使用的IDE是VC6,0, 请你下载Service Pack 6, 请看以下连接地址: http://www.microsoft.com/downloads/details.aspx?FamilyID=a8494edb-2e89-4676-a16a-5c5477cb9713&displaylang=en 另外,我强烈建议大家下载 Dev 编译器,这款编译器尽管有很多使用不便之处,但是用于测试程序真确与否,确是一款非常有效的编译器。

自由,民主,平等,博爱,进步.
中华民国,我的祖国,中华民国万岁!中华民国加油!
本人自愿加入中国国民党,为人的自由性,独立性和平等性而奋斗!
2004-12-28 13:58
三少爷
Rank: 1
等 级:新手上路
帖 子:192
专家分:0
注 册:2004-4-29
收藏
得分:0 
多谢kai斑竹!可是我现在用的是拨号,看来还得费一番周折了。

[此贴子已经被作者于2004-12-28 23:07:48编辑过]



2004-12-28 23:06
快乐如风
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2005-1-5
收藏
得分:0 
我看看
2005-01-05 10:38
快速回复:运算符重载问题
数据加载中...
 
   



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

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