【问题标题】:testing "Try and Catch"测试“Try and Catch”
【发布时间】: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

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


【解决方案1】:

虽然这个问题有些晦涩,但不妨试试这些建议。

首先,XArray&lt;&gt;::IndexOutOfBounds 可能没有适当的复制 ctor。您可以尝试通过 const 引用来捕获解决方法:

try
{
    ...
}
catch(const XArray<double>::IndexOutOfBound& e)
{
    e.print();
}

标准库容器中的索引运算符不检查边界,有一个特殊的 getter 进行检查,称为 at()。如果 XArray 类在设计时考虑到标准库,它的行为可能类似。

但是,要获得更充分的响应,您需要更具体地描述您遇到的问题。

【讨论】:

    【解决方案2】:

    我仍然想知道确切的问题是什么。 但是,我理解的问题是如何通过使用“IndexOutOfBound”来使用“catch”。

    #include <exception>
    #include <iostream>
    
    using namespace std;
    
    template <typename T>
    class Array
    {
    private:
        int m_nLength;
        T *m_ptData;
    
    public:
    ...
    ...
    
        T& operator[](int nIndex)
        {
            //assert(nIndex >= 0 && nIndex < m_nLength);
    
            if(nIndex < 0 || nIndex > m_nLength)
            {
                throw myex;
            }
            else
            {
                return m_ptData[nIndex];
            }
    
        }
    
        //class definition for 'IndexOutOfBound'
        class IndexOutOfBound: public exception
        {
        public:
            virtual const char* print() const throw()
            {
                return "Exception occured 'Index Out Of Bound'";
            }
        }myex;
    };
    
    int main()
    {
        Array<double> arr(3);
        try
        {
            arr[0] = 1;
            //exception will occur here.
            arr[4] = 2;
    
        }
        catch(Array<double>::IndexOutOfBound &e)
        {
            cout << e.print() << '\n';
        }
    
    
        return 0;
    }
    

    这里没有'XArray.h',所以我写了一个示例数组类。

    【讨论】:

      【解决方案3】:

      问题出在operator[] 函数中。代码idx = 0idx 设置为0。因此,您对operator[] 的所有调用都将返回第一个元素,因此除非数组为空,否则不会出现越界错误。

      你可能打算写if ( idx &gt;= 0 &amp;&amp; idx &lt; size )

      顺便说一句,throw 会中止该功能,returnthrow 之后没有任何意义。

      【讨论】:

      • 另外,在XArray&lt;T&gt;::init() 中,考虑将Array[i] - 0; 更改为Array[i] = 0;。更好的是完全删除循环,并使用 Array = new T[size](); 将整型数组初始化为零。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-29
      • 2016-02-19
      相关资源
      最近更新 更多