我有一个非常简单的程序,用于在数组模板类中定义*运算符.当我尝试编译它给我一个错误"非法间接".任何有关此事的帮助将不胜感激!
这是运营商的定义:
template <typename T>
NumericArray<T> NumericArray<T>::operator * (const int factor) const
{
NumericArray<T>* TempArray2 = new NumericArray<T>(Size());
for (int i=0; i<Size(); i++)
{
*TempArray2[i] = ((GetElement(i))*(factor));
}
return *TempArray2;
}
Run Code Online (Sandbox Code Playgroud)
这是测试主要功能的实现:
cout<<((*intArray1)*5).GetElement(0);
cout<<((*intArray1)*5).GetElement(1);
cout<<((*intArray1)*5).GetElement(2);
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
我有一小段代码在编译时给出了以下警告:
'BGOLUB :: Containers :: Stack :: Pop':并非所有控制路径都返回一个值
这是代码:
template<typename T>
T Stack<T>::Pop()
{
try
{
if (m_index<0) throw OutOfBoundsException(m_index);
--m_index;
return(m_array[m_index]);
}
catch(OutOfBoundsException&)
{
cerr<<"Underflow Index = "<<m_index<<endl;
}
catch(...)
{
cerr<<"Unhandled Error Occured"<<endl;
}
}
Run Code Online (Sandbox Code Playgroud)
有什么建议?
非常感谢!
我有一个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;
}
}
Run Code Online (Sandbox Code Playgroud)
我没有看到通过我的代码可以达到除定义结果之外的任何其他内容.
谢谢.