2016-03-02 140 views
-1

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

} 
+3

'getX'和'getY'是* functions *,不是成員。你需要使用函數調用語法,*例如*,'getX()' –

+2

不要解釋你所得到的錯誤。按照它們的顯示覆制粘貼它們。 –

+0

我收到以下內容: 錯誤(活動)\t \t函數模板「std :: distance」的實例與參數列表相匹配 - 標識符cout未識別 – Goblinette

回答

4

你必須通過每個名稱後添加()調用的方法getXgetY

return(sqrt(pow(point1.getX()-point2.getX(),2) + pow(point1.getY()-point2.getY(),2))); 

否則你將被減去函數指針,這是不允許的。

0

用類替換distance函數。在你的情況下std::distance被調用。並且儘量不要在你的代碼中使用using namespace std;

+1

更好的解決方案是刪除'using namespace std;' –

+0

是否有解釋爲什麼使用名稱空間std是錯誤的?我意識到std ::語法,但它並沒有用於我所教的地方。是什麼讓一個比另一個更好? – Goblinette

+0

這沒有錯。但是使用名稱空間標準可能會導致大型項目或使用庫時出錯(這是您的情況)。 – malchemist

1
#include <iostream> 
#include <math.h> 
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;}}; 

static double distance1(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:" << Point::distance1 (p2, p1) << endl; 

} 
+0

指出您已將距離作爲靜態成員函數,並通過'Point :: distance'調用它。 –

+1

對,不好意思。我的教授開始上課,我不得不關閉筆記本電腦。 –