Whist compiling the following code from this S/O answer, I keep getting errors due to binding issues.
class my_matrix {
std::vector<std::vector<bool> >m;
public:
my_matrix(unsigned int x, unsigned int y) {
m.resize(x, std::vector<bool>(y,false));
}
class matrix_row {
std::vector<bool>& row;
public:
matrix_row(std::vector<bool>& r) : row(r) {
}
bool& operator[](unsigned int y) {
return row.at(y);
}
};
matrix_row& operator[](unsigned int x) {
return matrix_row(m.at(x));
}
};
// Example usage
my_matrix mm(100,100);
mm[10][10] = true;
Here is the report
m.cpp:16:14: error: non-const lvalue reference to type 'bool' cannot bind to a
temporary of type 'reference' (aka '__bit_reference<std::__1::vector<bool,
std::__1::allocator<bool> > >')
return row.at(y);
^~~~~~~~~
m.cpp:20:12: error: non-const lvalue reference to type 'my_matrix::matrix_row'
cannot bind to a temporary of type 'my_matrix::matrix_row'
return matrix_row(m.at(x));
^~~~~~~~~~~~~~~~~~~
Having researched into this, I realise that a Bool vector is not the same as a normal c++ vector. Therefore, I can avoid the first error by changing it to an int vector.
The second error in the last line is a little more confusing. I have looked at this question but I still can't figure out what to do.
** Edit **
Given the answer/comment, I feel like something like this should work,
matrix_row& operator[](unsigned int x) {
std::vector<int> e = m.at(x);
matrix_row f = matrix_row(e);
return f;
It does not. This appears to create variables with memory though (e and f)?