我正在创建一个类来打印一个点,比较两个点以查看它们是否相等,并使用单独的方法找出两个点之间的距离。找到两点之间的距离的方法给了我一个类型错误,我不知道为什么。函数类型错误C++
#include <iostream>
using namespace std;
class Point
{//in C++ stuff is private by default
public:
Point() { double x = 0; double y = 0; }
Point (double a, double b) { x = a; y = b; }
void print() { cout << "(" << x << "," << y << ")\n" << endl; }
double getX() { return x; };
double getY() { return y; };
bool compare(Point other) { return (x == other.x && y == other.y); }
void setX(double a)
{if (a >= 0)
{x = a;}};
void setY(double b)
{if (b >= 0)
{y = b;}};
double distance(Point point1, Point point2)
{
return(sqrt (pow (point1.getX-point2.getX,2) + pow(point1.getY-point2.getY,2)));
};
private:
double x, y;
};
bool Compare(Point a, Point b) { return (a.getX() == b.getX()) && (b.getY() == a.getY()); }
int main()
{
Point p1(5,1);
Point p2;
p2.setX(2);
p2.setY(5);
p1.print();
p2.print();
p1.getX();
p1.getY();
p2.getX();
p2.getY();
p1.setX(3.5);
p1.setY(9);
p1.print();
p1.compare(p2);
//or p2.equals(p1);
distance(p1, p2);
cout << "This distance b/w p1 & p2 is:" << distance (p2, p1) << endl;
}
'getX'和'getY'是* functions *,不是成员。你需要使用函数调用语法,*例如*,'getX()' –
不要解释你所得到的错误。按照它们的显示复制粘贴它们。 –
我收到以下内容: 错误(活动)\t \t函数模板“std :: distance”的实例与参数列表相匹配 - 标识符cout未识别 – Goblinette