I want to overload the operators + and = for my matrix class. My code is:
friend float* operator+(const Matrix& m)
{
for(int i = 0; i < (int)(m._n*m._n); i++)
_m[i] = _m[i] + m._m[i];
};
friend Matrix& operator=(const Matrix& m)
{
std::swap(_m, m._m);
return *this;
};
with _m
the data and _n
the size of the square matrices. But my compiler gives me the following errors:
main.cpp:161:45: error: ‘Matrix& operator=(const Matrix&)’ must be a nonstatic member function
main.cpp: In function ‘float* operator+(const Matrix&)’:
main.cpp:192:12: error: invalid use of non-static data member ‘Matrix::_m’
main.cpp:158:13: error: from this location
main.cpp:192:12: error: invalid use of non-static data member ‘Matrix::_m’
main.cpp:158:21: error: from this location
main.cpp:159:5: warning: no return statement in function returning non-void [-Wreturn-type]
For the first error, I have read that it has to be directly in the class, but even when I put it in there, I still get the error. For the second error, I have no idea how to solve this. What am I doing wrong?
Thank you!