0

In the following, I would like to define valid neighbours as a class member attribute:

struct Chessboard
{ 
    ...

    vector<vector<Cell>> cells;

    static int constexpr neighbours[][2] = {
         {-1, -1}, {-1, 0}, {-1, 1},
         {0, -1},  {0, 0},  {0, 1},
         {1, 1},   {1, 0},  {1, 1}
    };

    bool isNeighbour() {
        for (int row = 0; row < nRows; row++) {
            for (int col = 0; col < nCols; col++) {
                for (const int *d : neighbours) { // <---- Error!
                    if (cells[row][col].isMagic) {
                        return true;
                    }
                }
            }
        }
    }
};

Unfortunately, I get an undefined reference to Chessboard::neighbours` during the link process.

What would be the correct way of doing it?

Looking at this question it seems static members should be declared inside the .cpp file, but in my case, I get the exact same behaviour.

For instance, in Python I can declare static members inside my class. Why isn't allowed in C++?

nowox
  • 25,978
  • 39
  • 143
  • 293

0 Answers0