C++ 函数类型错误c++;

C++ 函数类型错误c++;,c++,class,points,C++,Class,Points,我正在创建一个类来打印一个点,比较两个点,看它们是否相等,并为每个点使用单独的方法来查找两个点之间的距离。求两点之间距离的方法给了我一个类型错误,我不知道为什么 #include <iostream> using namespace std; class Point {//in C++ stuff is private by default public: Point() { double x = 0; double y = 0; } Point (double

我正在创建一个类来打印一个点,比较两个点,看它们是否相等,并为每个点使用单独的方法来查找两个点之间的距离。求两点之间距离的方法给了我一个类型错误,我不知道为什么

    #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;

}
#包括
使用名称空间std;
类点
默认情况下,{//C++中的东西是私有的
公众:
点(){double x=0;double y=0;}
点(双a,双b){x=a;y=b;}

void print(){cout您必须通过在每个名称后面添加
()
来调用方法
getX
getY

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

否则,您将减去指向函数的指针,这是不允许的。

从类中替换
distance
函数。在您的情况下,调用
std::distance
。并尝试在代码中根本不要使用
使用命名空间std;

\35;
#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;

}
#包括 使用名称空间std; 类点 默认情况下,{//C++中的东西是私有的 公众: 点(){double x=0;double y=0;} 点(双a,双b){x=a;y=b;}
void print(){cout
getX
getY
是函数,而不是成员。您需要使用函数调用语法,例如,
getX()
不要解释您得到的错误。复制粘贴它们,就像它们显示的那样。我得到以下信息:错误(活动)没有函数模板的实例“std::distance”匹配参数列表-标识符cout无法识别您将
距离
设置为成员函数。可以将其设置为具有两个参数的自由函数,也可以将其设置为具有一个参数的成员(如
比较
)。在构造函数中初始化成员比分配成员更为简洁:So
点(双a,双b):x(a),y(b){}
。此外,还应该使getX和getY常量成员:
双getX()常量{return x;}
。compare也应该是const,您需要考虑一下比较双精度以获得精确相等的智慧。它可以工作,但也会导致大量的绘制。更好的解决方案是删除使用namespace std的
是否有解释为什么使用namespace std是错误的?我知道std::语法,但它是错误的在我被教导的地方没有使用。是什么让一个比另一个更好?这没有错。但是
使用namespace std;
在大型项目或使用库时可能会导致错误(这是你的情况)@nyx这里有一个很好的例子。有一个
std::distance
函数,你自己定义了一个
distance
函数。这是一个潜在的名称冲突,防止名称冲突是名称空间存在的全部理由。键入
std::
比处理这些问题容易得多。请参阅also:请您指出,您已经将
distance
设置为一个静态成员函数,并通过
point::distance
调用它。是的,对不起,伙计。我的教授开始上课了,我不得不关闭笔记本电脑。