#include<iostream>
#include<cmath>
using namespace std;
class Point{
public:
Point(float x,float y)
{
pointx=x;
pointy=y;
}
void showpoint()
{
cout<<"点的坐标为:("<<pointx<<","<<pointy<<")"<<endl;
}
friend float dist(point &a,point &b);
private:
float pointx;
float pointy;
};
float dist(point &a,point &b)
{
return sqrt((a.pointx-b.pointx)*(a.pointx-b.pointx)+(a.pointy-b.pointy)*(a.pointy-b.pointy));
}
int main()
{
Point p1(3,3);
p1.showpoint();
Point p2(0,0);
p2.showpoint();
dist(p1,p2);
cout<<"两点之间的距离是:"<<dist(p1,p2)<<endl;
return 0;
}
下面是错误:
p152_5.cpp(15) : error C2061: syntax error : identifier “point”
p152_5.cpp(21) : error C2065: “point” : undeclared identifier
p152_5.cpp(21) : error C2065: “a” : undeclared identifier
p152_5.cpp(21) : error C2065: “b” : undeclared identifier
p152_5.cpp(22) : error C2448: “<Unknown>” : function-style initializer appears to be a function definition
p152_5.cpp(33) : error C2660: “dist” : function does not take 2 parameters
p152_5.cpp(34) : error C2660: “dist” : function does not take 2 parameters
能不能详细解答一下?
题目是设计一个点类,其中包括一对坐标点数据成员,一个求两个点之间距离的友元函数dist和显示坐标点的成员函数,且用数据进行测试
解决方案
10
(point &a,point &b)
类名跟你定义的不一样,一个大写一个小写
类名跟你定义的不一样,一个大写一个小写
20
大小写写错了,有几个Point写成了point
#include<iostream>
#include<cmath>
using namespace std;
class Point{
public:
Point(float x,float y)
{
pointx=x;
pointy=y;
}
void showpoint()
{
cout<<"点的坐标为:("<<pointx<<","<<pointy<<")"<<endl;
}
friend float dist(Point &a,Point &b);
private:
float pointx;
float pointy;
};
float dist(Point &a,Point &b)
{
return sqrt((a.pointx-b.pointx)*(a.pointx-b.pointx)+(a.pointy-b.pointy)*(a.pointy-b.pointy));
}
int main()
{
Point p1(3,3);
p1.showpoint();
Point p2(0,0);
p2.showpoint();
dist(p1,p2);
cout<<"两点之间的距离是:"<<dist(p1,p2)<<endl;
return 0;
}