2016-03-02 143 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

对,不好意思。我的教授开始上课,我不得不关闭笔记本电脑。 –