Generic container and constructor/destructor

If I have a template class as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
template<class T> class Stack_Template
{
	T* v;
	int top;
	int max_size;

public:
	// Exception/error handling
	class Underflow{};				// Used as exception (class = type)
	class Overflow{};				// Used as exception
	class Bad_Size{};				// Used as exception

	Stack_Template(int s);			// Constructor
	~Stack_Template();				// Destructor (used for cleanup when an object of the class goes out of scope)

	void push(T);
	T pop();
};


How do I create the constructor/destructor subroutines correctly?

Here's what I have so far but they do not work:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// Constructor
template<class T> Stack_Template::Stack_Template(int s)
{
	top = 0;
	if (s < 0 || s > 10000)
	{
		throw Bad_Size();
	}
	max_size = s;
	v = new T[s];		// allocate elements on the free store (heap, dynamic store)
}

// Destructor (Frees memory)
template<class T> Stack_Template::~Stack_Template()
{
	delete[] v;				// Free the elements for possible reuse of their space
}


Thank you.
Last edited on
Why do you say they don't work? They look ok to me.
Error 1 error C2955: 'Stack_Template' : use of class template requires template argument list

AND

Error 2 error C2244: 'Stack_Template<T>::Stack_Template' : unable to match function definition to an existing declaration
1
2
3
4
5
6
7
8
9
template<class T> Stack_Template<T>::Stack_Template(int s)
{
 /* snip */
}

template<class T> Stack_Template<T>::~Stack_Template()
{
 /* snip */
}

That solved it, thank you!

I sometimes get an error in the form of the following (nothing to do with this topic):

Error 1 general error c101008d: Failed to write the updated manifest to the resource of file "..\Debug\C++ Project.exe". Access is denied. mt.exe

Seems odd, but it doesn't occur every time.
That's apparently a random bug with VC++...try rebuilding the project completely.

Oh, and make your exception classes derive from std::exception.

Oh, and make your exception classes derive from std::exception.


Can you please give me an example? Say I have the following class in my header file:

 
class Overflow{};				// Used as exception 


How would I make sure it is derived from std::exception?
How would I make sure it is derived from std::exception?


1
2
3
#include <exception>

class Overflow : public std::exception{};
Thanks for helping a newb out guys. Much appreciated! :D

EDIT:

My header file now looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <exception>

template<class T> class Stack_Template
{
	T* v;
	int top;
	int max_size;

public:
	// Exception/error handling
	class Underflow : public std::exception{};		// Used as exception (class = type)
	class Overflow : public std::exception{};		// Used as exception
	class Bad_Size : public std::exception{};		// Used as exception

	Stack_Template(int s);			// Constructor
	~Stack_Template();				// Destructor (used for cleanup when an object of the class goes out of scope)

	void push(T);
	T pop();
};	


I hope that is OK! Please shout if not.
Last edited on
Eh, well std::exception has an annoying destructor declaration.

You have to do this:

1
2
3
4
class Underflow : public std::exception {
    public:
        virtual ~Underflow() throw() {}
};


at a minimum.

At a minimum?! :( Is there a guide to using exceptions in this way as I fear I much to learn.
Not really much.

Typically you'll want to customize the error string returned by std::exception::what(),
and to do that you might consider providing a forwarding constructor for Underflow
(that takes the error message).
Topic archived. No new replies allowed.