我正在創建一個類來打印一個點,比較兩個點以查看它們是否相等,並使用單獨的方法找出兩個點之間的距離。找到兩點之間的距離的方法給了我一個類型錯誤,我不知道爲什麼。函數類型錯誤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