如果有人愿意,我可以使用一些专业知识调试我的代码。我试图从类文件中调用函数,但我遇到了一些错误。我正在学习c ++的过程中。我的错误如下:
In function 'int main()':
error: no matching function for call to 'MyPoint::distance(MyPoint&)'
note: candidate is:
note: double MyPoint::distance(double)
Main.cpp的
#include <iostream>
#include <cmath>
#include "MyPoint.h"
using namespace std;
int main()
{
MyPoint point1;
MyPoint point2(10.2, 34.8);
cout << point1.distance(point2);
return 0;
}
MyPoint.h
#ifndef MYPOINT_H
#define MYPOINT_H
using namespace std;
class MyPoint
{
public:
MyPoint();
MyPoint(double, double);
double getX();
double getY();
double distance(double);
private:
double x, y;
};
#endif // MYPOINT_H
MyPoint.cpp
#include <iostream>
#include <cmath>
#include "MyPoint.h"
using namespace std;
MyPoint::MyPoint()
{
x = 0.0;
y = 0.0;
}
MyPoint::MyPoint(double x1, double y1)
{
x = x1;
y = y1;
}
double MyPoint::getX()
{
return x;
}
double MyPoint::getY()
{
return y;
}
double MyPoint::distance(double p2)
{
return sqrt((x - p2.x) * (x - p2.x) + (y - p2.y) * (y - p2.y));
}
谢谢...
答案 0 :(得分:1)
你宣布distance
为:
double distance(double);
这意味着MyPoint::distance
方法需要double
,而不是MyPoint
。看起来你可以改变说明,它可能会起作用。
在标题中:
double distance(MyPoint&);
和您的实施:
double MyPoint::distance(MyPoint& p2)
答案 1 :(得分:0)
您的double MyPoint::distance(double p2)
定义错误。它应该会收到MyPoint
而不是double
答案 2 :(得分:0)
您需要更改MyPoint类中的距离方法:
在.h
中声明double distance(const MyPoint& p2);
在.cpp中实现
double MyPoint::distance(const MyPoint& p2)
{
return sqrt((x - p2.x) * (x - p2.x) + (y - p2.y) * (y - p2.y));
}