Domanda

In this program, I am using template class, I have a header file and this is my main file. I am having trouble displaying the (".....") IndexOutOfBounds and displaying it on the screen.

#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;
}

There is the Array.h file posted as a dropbox link

Array.h

The code for operator[] in Array.h is:

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();
    }
}
È stato utile?

Soluzione 3

The problem is in the operator[] function. The code idx = 0 sets idx to 0. So all of your calls to operator[] will return the first element, and therefore there is no out-of-bounds error unless the array is empty.

You probably meant to write if ( idx >= 0 && idx < size ).

BTW the throw aborts the function, it makes no sense to return after throw.

Altri suggerimenti

Although the question is somewhat obscure, give a try to these suggestions.

Firstly, it can happen that XArray<>::IndexOutOfBounds have no proper copy ctor. You can try catching by const reference to workaround that:

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

Index operator in standard library containers does not check for bounds, there is a special getter that does the check called at(). If the XArray class is designed with standard library in mind, it could behave similarly.

However to get more adequate response you need to be more specific describing the trouble you are having.

I'm still wondering what exact question is. However, I'm understanding the question is that how I can use 'catch' by using 'IndexOutOfBound'.

#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;
}

Here is no 'XArray.h', so I've written a sample array class for example.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top