【发布时间】:2014-05-07 04:06:48
【问题描述】:
在这个程序中,我使用模板类,我有一个头文件,这是我的主文件。我无法显示 (".....") IndexOutOfBounds 并将其显示在屏幕上。
#include "XArray.h"
#include <iomanip>
#include <string>
using namespace std;
template<class T>
void afriend ( XArray<T> );
int main()
{
XArray<double> myAD(18);
myAD.randGen(15, 100);
cout << myAD.getType() << endl;
cout << setprecision(1) << fixed << "\n\n Unsorted: " << myAD;
myAD.sort();
cout << "\n Now Sorted: " << myAD;
cout << "\n\n";
**try
{
cout << "A[-5] = " << setw(6) << myAD[-5] << endl;
}
catch(XArray<double>::IndexOutOfBound e)
{
e.print();
}
try
{
cout << "A[8] = " << setw(6) << myAD[8] << endl;
}
catch(XArray<double>::IndexOutOfBound e)
{
e.print();
}**
cout << "\n\n" << setprecision(2) << fixed;
cout << "Size = " << setw(6) << myAD.getSize() << endl;
cout << "Mean = " << setw(6) << myAD.mean() << endl;
cout << "Median = " << setw(6) << myAD.median() << endl;
cout << "STD = " << setw(6) << myAD.std() << endl;
cout << "Min # = " << setw(6) << myAD.min() << endl;
cout << "Max # = " << setw(6) << myAD.max() << endl;
return 0;
}
Array.h 文件作为 Dropbox 链接发布
Array.h 中operator[] 的代码为:
template <class T>
T XArray<T>::operator[] (int idx)
{
if( (idx = 0) && (idx < size) )
{
return Array[idx];
}
else
{
throw IndexOutOfBound();
return numeric_limits<T>::epsilon();
}
}
【问题讨论】:
-
究竟是什么意思? '我无法显示 (".....") IndexOutOfBounds。'
-
@CodeDreamer 你看到两个**从哪里开始和结束了吗??我无法显示该内容
-
那么,你想通过使用'IndexOutOfBound'来使用'catch'吗?
-
您必须出示
XArray.h文件 -
@CodeDreamer 我已经发布了 XArray.h 文件。
标签: c++ visual-studio-2012 template-classes