
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 时出错.
请高手指点下怎么回事 展开
#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 时出错.
请高手指点下怎么回事 展开
展开全部
粗心了
friend float fdist(point &a,point &b); // 声明是写错了第二个参数类型
friend float fdist(point &a,point &b); // 声明是写错了第二个参数类型
展开全部
你的友元函数声明的有问题:
friend float fdist(point &a,float &b);应该是:friend float fdist(point &a,point &b);
friend float fdist(point &a,float &b);应该是:friend float fdist(point &a,point &b);
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
不能直接访问私有成员,你不是有getX()吗
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询