I have just started with C++ and am stuck on the move constructor. Here is my .cpp
:
SimpleMatrix::SimpleMatrix(SimpleMatrix &&other_mat) {
cols = other_mat.cols;
rows = other_mat.rows;
data_ = other_mat.data_;
other_mat.cols = 0;
other_mat.rows = 0;
other_mat.data_ = nullptr; // <- Error here
}
I got No viable overloaded =
error at other_mat.data_ = nullptr
. What went wrong? Is it the way I initialize the matrix?
Here is the relevant parts in .hpp
file:
class SimpleMatrix {
public:
SimpleMatrix(std::size_t nrows, std::size_t ncols);
SimpleMatrix(std::initializer_list<std::initializer_list<double>> data);
SimpleMatrix(SimpleMatrix&& other_mat);
SimpleMatrix& operator=(SimpleMatrix&& other_mat);
private:
std::vector<std::vector<double> > data_;
int rows, cols;
};