hi what should I do when WARANING “control reaches end of non-void function” happens ?
my overloaded operator has try and catch and returns *this ; in the try scope.
I’m using Eclipse , G++ is the compiler, UBUNTU linux
NNmatrix & operator*=(const NNmatrix<T> &mtrxB)
{
// A=2*3 B=3*4 return C=2*4
const UINT aRows = this->size1();
const UINT aCols = this->size2();
const UINT bRows = mtrxB.size1();
const UINT bCols = mtrxB.size2();
try
{
// if cols of first(this) matrix == rows of second matrix
if (aCols != bRows) throw bad_alloc();
const UINT cRows = aRows;// = rows of first matrix
const UINT cCols = bCols; // = cols of second matrix
NNmatrix mtrxC(cRows, cCols);
T val;
for (UINT i = 0; i < cRows; i++)
{
for (UINT j = 0; j < cCols; j++)
{
val = 0;
for (UINT k = 0; k < bRows; k++)
{
val += this->matrix.at(i).at(k) * mtrxB.getElement(k, j);
}
mtrxC.setElement(i, j, val);
}
}
*this = mtrxC;
mtrxC.clear();
return *this;
}
catch (exception& e)
{
cout<<"Dimension don't match: ("<<aRows<<","<<aCols<<") ("<<bRows<<","<<bCols<<")"<<endl;
}
}
You need to make sure all code paths return a value if the function returns anything but
void.If you are handling exceptions internally to this function without rethrowing, why not just
return *this;at the end of the function unconditionally, instead of from inside thetryblock?EDIT: per @Mark’s comment below, simply moving the return statement hides an error that is fatal in the context of the requested operation, and makes the library rather unreliable in the process. Better to propagate the exception, if that’s how you are going to handle in-place multiplication errors (which seems a reasonable approach).