【发布时间】:2010-11-22 16:20:41
【问题描述】:
嗨,当警告“控制到达非无效函数的结尾”发生时我该怎么办?
我的重载运算符在 try 范围内有 try 和 catch 和 returns *this ;。
我用的是Eclipse,G++是编译器,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;
}
}
【问题讨论】:
-
请发布您的代码(以及您使用的编译器)。
-
您应该修复警告。现在,你有一个例子来说明你的问题吗?
-
请贴出你的代码,我从你的问题中得到了一个模糊的概念。
-
你在 catch 范围内返回 smth 吗?
-
好的,先谢谢了
标签: c++ exception return-value try-catch