c++关于用类作为类中函数的参数问题
我写了一个头文件和一个CPP文件,请问错误在哪里?MyPoint.h:classMyPoint{public:MyPoint();MyPoint(doublex,doub...
我写了一个头文件和一个CPP文件,请问错误在哪里?
MyPoint.h:
class MyPoint
{
public:
MyPoint();
MyPoint(double x,double y,double z);
double getX();
double getY();
double getZ();
double distance(MyPoint point());
private:
double x;
double y;
double z;
};
MyPoint.cpp:
#include"MyPoint.h"#include<cmath>
MyPoint::MyPoint()
:x(0),y(0),z(0)
{
}
MyPoint::MyPoint(double x,double y,double z)
{
this->x=x;
this->y=y;
this->z=z;
}
double MyPoint::getX()
{
return x;
}
double MyPoint::getY()
{
return y;
}
double MyPoint::getZ()
{
return z;
}
double MyPoint::distance(class MyPoint point())
{
double a=(x-point.getX())+(y-point.getY())+(z-point.getZ());
return sqrt(a);
} 展开
MyPoint.h:
class MyPoint
{
public:
MyPoint();
MyPoint(double x,double y,double z);
double getX();
double getY();
double getZ();
double distance(MyPoint point());
private:
double x;
double y;
double z;
};
MyPoint.cpp:
#include"MyPoint.h"#include<cmath>
MyPoint::MyPoint()
:x(0),y(0),z(0)
{
}
MyPoint::MyPoint(double x,double y,double z)
{
this->x=x;
this->y=y;
this->z=z;
}
double MyPoint::getX()
{
return x;
}
double MyPoint::getY()
{
return y;
}
double MyPoint::getZ()
{
return z;
}
double MyPoint::distance(class MyPoint point())
{
double a=(x-point.getX())+(y-point.getY())+(z-point.getZ());
return sqrt(a);
} 展开
3个回答
展开全部
double distance(MyPoint point());
定义类内部的函数不需要构造对象,把point后面的括号去掉
double distance(MyPoint point);
当然写成:double distance(const MyPoint &point) const;更好
double MyPoint::distance(class MyPoint point())
这个去掉class,point后面的括号
double MyPoint::distance(MyPoint point)
同样变成:double MyPoint::distance(const MyPoint &point) const更好
定义类内部的函数不需要构造对象,把point后面的括号去掉
double distance(MyPoint point);
当然写成:double distance(const MyPoint &point) const;更好
double MyPoint::distance(class MyPoint point())
这个去掉class,point后面的括号
double MyPoint::distance(MyPoint point)
同样变成:double MyPoint::distance(const MyPoint &point) const更好
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
求两点间距离的公式用错了,参数貌似也不对,建议改成这样:
double MyPoint::distance(MyPoint &mp) {
double a = (x - mp.getX()) * (x - mp.getX()) + (y - mp.getY())*(y - mp.getY()) + (z - mp.getZ()) * (z - mp.getZ());;
return sqrt(a);
}
double MyPoint::distance(MyPoint &mp) {
double a = (x - mp.getX()) * (x - mp.getX()) + (y - mp.getY())*(y - mp.getY()) + (z - mp.getZ()) * (z - mp.getZ());;
return sqrt(a);
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
MyPoint.h:
double distance(MyPoint point());
改为
doubledistance(MyPoint point);
MyPoint.cpp:
double MyPoint::distance(class MyPoint point())
改为
doubleMyPoint::distance(MyPoint point)
另外:这个方法的算法不太对。
double distance(MyPoint point());
改为
doubledistance(MyPoint point);
MyPoint.cpp:
double MyPoint::distance(class MyPoint point())
改为
doubleMyPoint::distance(MyPoint point)
另外:这个方法的算法不太对。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询