I'm trying to return the result of operator+ to operator= but am getting a bogus value when returning *this in operator =; when calling rhs and accessing its functions the values are correct, but operator = is not sending out a copy of the class whats wrong?
VecXd& operator=(const VecXd &rhs) { cout << rhs.vecArr[0] << " " << rhs.vecArr[1] << " " << rhs.vecArr[2] << " " << endl; return *this; } VecXd& operator+(const VecXd& rhs){ VecXd& result = *this; cout << "TEST FOR DIMENSION-> " << dimension << endl; if(result.dimension == rhs.dimension) //dimension level check { for(int i = 0; i < rhs.dimension; i++) { result.vecArr[i] += rhs.vecArr[i]; cout << result.vecArr[i] << " our new value" << endl; } cout << result << " result test!" << endl; return result; } else{ cout << "Dimensions do not match!!! Error!" << endl; } }
Help? thank you!
VecXd &result = *this;instead ofVecXd result = *this;?if (dimension == rhs.dimension)check. If the code skips to else, the function no longer returns a value.resultlike this:VecXd &result = *this;would be right if you were implementing the += operator, but you are not.