无效的构造函数和运算符

时间:2017-02-21 02:29:40

标签: c++

struct Point
{
    int x;
    int y;

    Point() { x = NULL; y = NULL; }
    Point(int x1, int y1) { x = x1;     y = y1; }

    ~Point(void) {  }


    Point & operator= (const Point &p)
    {
        x = p.x;    y = p.y;    return (*this);
    }

    bool operator== (const Point &p)
    {
        return ((x == p.x) && (y == p.y));
    }

    bool operator!= (const Point &p)
    {
        return ((x != p.x) || (y != p.y));
    }
}

我的变量类型Point的代码

下面是我的cpp的简短代码

Point Finish = ladyrinth->getEndLocation(); // This is to get the coordinate of end location
bool up = true;
bool down = true;
bool left = true;
bool right = true;

if (up == Finish)
        DirectionMove = 0;
    if (down == Finish)
        DirectionMove = 1;
    if (left == Finish)
        DirectionMove = 2;
    if (right == Finish)
        DirectionMove = 3;

错误代码不是运算符" =="匹配操作数,操作数是bool to point 但在我尝试制作操作符后,他们说没有我不知道如何制作的构造函数。请帮帮我。

1 个答案:

答案 0 :(得分:1)

没有重载的operator==可以与boolPoint一起使用。编译器尝试隐式地将bool强制转换为Point,但Point没有一个构造函数,其中boolPoint不会生成operator bool有隐式演员operator==。创建一个重载的//in class definition friend bool operator==(bool, const Point &); //outside class definition bool operator==(bool b, const Point &p) { return something; }

{{1}}