我有一个 if...else 语句,并且在编译时收到警告“并非所有控制路径都返回一个值”。谁能告诉我为什么会收到此警告?
我的代码:
template<typename T>
double NumericArray<T>::Dot(const NumericArray& na)
{
if (Size() == na.Size())
{
double result = 0;
for (int i=0; i<Size(); i++)
{
result += ((na.GetElement(i))*(GetElement(i)));
}
return result;
}
else
{
cout<<"Error! Dot Product Operands Number Of Elements Unequal"<<endl;
}
}
除了定义的结果之外,我看不出如何通过我的代码实现任何其他结果。
谢谢。
最佳答案
因为在你的最终 else 中或 if block 之外没有 return 语句。
如果此条件 if (Size() == na.Size())
计算结果为 false,则该方法不会返回值。
关于c++ - 如果..否则警告 "not all control paths return a value",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16023437/