I wrote a struct to maintain a state of tic-tac-toe game.
struct Board{
std::vector<std::vector<int>> board;
Board() {
board = std::vector<std::vector<int>> (3, std::vector<int> (3));
}
std::vector<int> & operator[](int id) { return board[id]; }
int & operator[](std::pair<int, int> pos) { return board[pos.first][pos.second]; }
};
It works fine, and I can access the values within the struct in two ways:
Board position;
position[1][2] ++;
position[std::make_pair(0, 0)] --;
Then I realized that maybe it would be good to have some more general operator for two dimensional vector, to extract a value at some position given a pair of integers (something like this).
int & operator [] (std::vector<std::vector<int>> & vec, std::pair<int, int> & p) { return vec[p.first][p.second]; }
But then I was confronted with the following statement from the compiler:
stack.cpp:15:7: error: ‘int& operator[](std::vector<std::vector<int> >&, std::pair<int, int>&)’ must be a nonstatic member function
15 | int & operator [] (std::vector<std::vector<int>> & vec, std::pair<int, int> & p) { return vec[p.first][p.second]; }
| ^~~~~~~~
Is there a way, to implement something with a functionality described above?