lundi 26 juin 2017

std::map is not inserting struct elements [duplicate]

This question already has an answer here:

I have a struct Point

struct Point
{
    int x;
    int y;
    bool operator == (const Point& point) const {
        return this->x != point.x ? false : (this->y != point.y ? false : true);
    }

    bool operator < (const Point& point) const {
        return this->x < point.x && this->y < point.y;
    }
};

I also have a DraughtType enum

enum class DraughtType {
    NORMAL, QUEEN
};

And I have a std::map < Point,DraughtType >. When I insert elements into std::map, only 3 values are inserted.

This is the code for inserting white draughts

void Board::initializeWhiteDraughts()
{
    for (auto i = 0; i < 3; ++i) {
        int x = i;
        for (auto j = i % 2 == 0 ? 0 : 1 ; j < COLUMNS; j+=2) {
            Point point;
            point.x = x;
            point.y = j;
            whiteDraughts[point] = DraughtType::NORMAL;
        }
    }
}

But the output is the following:

0 0
1 1
2 2

It's clearly missing a lot of draughts.

What's wrong with my code? I've tried to change the operator comparison, but it didn't work.

Aucun commentaire:

Enregistrer un commentaire