Catching an exception class within a template

Posted by Todd Bauer on Stack Overflow See other posts from Stack Overflow or by Todd Bauer
Published on 2011-04-07T19:19:29Z Indexed on 2014/06/10 15:24 UTC
Read the original article Hit count: 433

I'm having a problem using the exception class Overflow() for a Stack template I'm creating. If I define the class regularly there is no problem. If I define the class as a template, I cannot make my call to catch() work properly. I have a feeling it's simply syntax, but I can't figure it out for the life of me.

#include<iostream>
#include<exception>
using namespace std;

template <class T>
class Stack
{
private:
    T *stackArray;
    int size;
    int top;

public: 
    Stack(int size) { this->size = size; stackArray = new T[size]; top = 0; }
    ~Stack() { delete[] stackArray; }

    void push(T value)
    {
        if (isFull())
            throw Overflow();
        stackArray[top] = value;
        top++;
    }

    bool isFull()
    {
        if (top == size)
            return true;
        else
            return false;
    }

    class Overflow {};

};

int main()
{
    try
    {
        Stack<double> Stack(5);
        Stack.push( 5.0);
        Stack.push(10.1);
        Stack.push(15.2);
        Stack.push(20.3);
        Stack.push(25.4);
        Stack.push(30.5);
    }
    catch (Stack::Overflow)
    {
        cout << "ERROR! The stack is full.\n";
    }

    return 0;
}

The problem is in the catch (Stack::Overflow) statement. As I said, if the class is not a template, this works just fine. However, once I define it as a template, this ceases to work. I've tried all sorts of syntaxes, but I always get one of two sets of error messages from the compiler.

If I use catch(Stack::Overflow):

ch18pr01.cpp(89) : error C2955: 'Stack' : use of class template requires template argument list ch18pr01.cpp(13) : see declaration of 'Stack' ch18pr01.cpp(89) : error C2955: 'Stack' : use of class template requires template argument list ch18pr01.cpp(13) : see declaration of 'Stack' ch18pr01.cpp(89) : error C2316: 'Stack::Overflow' : cannot be caught as the destructor and/or copy constructor are inaccessible

EDIT: I meant
If I use catch(Stack<double>::Overflow) or any variety thereof:

ch18pr01.cpp(89) : error C2061: syntax error : identifier 'Stack'
ch18pr01.cpp(89) : error C2310: catch handlers must specify one type
ch18pr01.cpp(95) : error C2317: 'try' block starting on line '75' has no catch handlers

I simply can not figure this out. Does anyone have any idea?

© Stack Overflow or respective owner

Related posts about c++

Related posts about templates