C++问题 ,求两点之间的距离

#include<iostream>#include<cmath>usingnamespacestd;classpoint{public:point(intxx=0,in... #include<iostream>
#include<cmath>
using namespace std;
class point
{
public:
point(int xx=0,int yy=0){X=xx;Y=yy;}
int getX(){return X;}
int getY(){return Y;}
friend float fdist(point &a,float &b);
private:
int X,Y;
};
float fdist(point &p1,point &p2)
{
double x=double(p1.X-p2.X);
double y=double(p1.Y-p2.Y);
return float(sqrt(x*x+y*y));
}
int main()
{
point myp1(2,3),myp2(4,5);
cout<<"结果是:"<<endl;
cout<<fdist(myp1,myp2)<<endl;
}

编译问题:
E:\C++程序\35\1.cpp(16) : error C2248: 'X' : cannot access private member declared in class 'point'
E:\C++程序\35\1.cpp(12) : see declaration of 'X'
E:\C++程序\35\1.cpp(16) : error C2248: 'X' : cannot access private member declared in class 'point'
E:\C++程序\35\1.cpp(12) : see declaration of 'X'
E:\C++程序\35\1.cpp(17) : error C2248: 'Y' : cannot access private member declared in class 'point'
E:\C++程序\35\1.cpp(12) : see declaration of 'Y'
E:\C++程序\35\1.cpp(17) : error C2248: 'Y' : cannot access private member declared in class 'point'
E:\C++程序\35\1.cpp(12) : see declaration of 'Y'
E:\C++程序\35\1.cpp(25) : warning C4508: 'main' : function should return a value; 'void' return type assumed
执行 cl.exe 时出错.

E:\C++程序\35\1.cpp(16) : error C2248: 'X' : cannot access private member declared in class 'point'
E:\C++程序\35\1.cpp(12) : see declaration of 'X'
E:\C++程序\35\1.cpp(16) : error C2248: 'X' : cannot access private member declared in class 'point'
E:\C++程序\35\1.cpp(12) : see declaration of 'X'
E:\C++程序\35\1.cpp(17) : error C2248: 'Y' : cannot access private member declared in class 'point'
E:\C++程序\35\1.cpp(12) : see declaration of 'Y'
E:\C++程序\35\1.cpp(17) : error C2248: 'Y' : cannot access private member declared in class 'point'
E:\C++程序\35\1.cpp(12) : see declaration of 'Y'
E:\C++程序\35\1.cpp(25) : warning C4508: 'main' : function should return a value; 'void' return type assumed
执行 cl.exe 时出错.

请高手指点下怎么回事
展开
 我来答
terranlong
2012-11-08 · TA获得超过7294个赞
知道大有可为答主
回答量:2660
采纳率:66%
帮助的人:4094万
展开全部
粗心了
friend float fdist(point &a,point &b); // 声明是写错了第二个参数类型
cjfeii
2012-11-08 · TA获得超过395个赞
知道小有建树答主
回答量:497
采纳率:0%
帮助的人:446万
展开全部
你的友元函数声明的有问题:
friend float fdist(point &a,float &b);应该是:friend float fdist(point &a,point &b);
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
jvzilkp
2012-11-08 · TA获得超过998个赞
知道小有建树答主
回答量:3070
采纳率:0%
帮助的人:1433万
展开全部
不能直接访问私有成员,你不是有getX()吗
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式