0

I have no idea, why I get SIGSEGV when return const reference from overloaded operator, but there is no problem when I just return value. Moreover I've checked my code at http://cpp.sh/ and it executed without error. I use Qt Creator 4.2.1 with MinGW 5.3 (but also compiled directly from command line with same result). Here is my code:

using TCardsTruthMatrix = std::vector<std::vector<bool>>;

struct CardsTruthMatrix_S
{
    CardsTruthMatrix_S(TCardsTruthMatrix matrix) : m_matrix(matrix) {}
    TCardsTruthMatrix m_matrix;
    const bool& operator()(unsigned x, unsigned y) //const bool operator-ok
    {
        return m_matrix.at(x).at(y);
    }
}; 

And how I get a value:

TCardsTruthMatrix handWithBoard;

for(int i = 0 ; i < 13; i++)
{
    std::vector<bool> vec(4, false);
    handWithBoard.push_back(vec);
}

CardsTruthMatrix_S board = handWithBoard;
if(!board(1,1)) // SIGSEGV here
{
    cout << "Zzzz" << endl;
}
Marcin_cyna
  • 97
  • 1
  • 1
  • 6

0 Answers0