My teacher issued an assignment for us to implement a stack and node class based off of his header files, and then to code a postfix expression calculator using the stack and node classes that we implement. When I tried to generate an instance of my stack class, VS threw me a linker error, citing "scalar deleting destructor". I tried commenting out the contents of one/both of my destructors, but the error persists. Can anyone give me a hand?
Node.h
template < class T>
class node
{
public:
T info; //member to store the data of the node
node<T>* next; //member to hold pointer to next node
node(); //default constructor
node(T); //constructor with 1 parameter for data
~node(); //destructor
};
Node.cpp
#include "Node.h"
//default constructor
template <class T>
node<T>::node () :
info (new T),
next (NULL)
{}
//constructor with 1 parameter for data
template <class T>
node<T>::node (T data)
{
info = new T data;
next = NULL;
}
// destructor
template <class T>
node<T>::~node()
{ delete info; }
Stack.h
#include "Node.h"
template < class T>
class Stack{
private:
node<T> * top; //pointer to the top node on the stack
int count; // keep track the number of items on stack
public:
Stack(void); //the "constructor"
~Stack(); //class destructor - free up used memory
void push(T a); // add (push) the given item a onto the stack
T pop(void); // returns and removes the top item on the stack
T peek(void); // returns the top item without removing it from stack
bool isEmpty() const; //return true if the stack has no elements
int get_count(); // returns the count of nodes in the stack
};
Stack.cpp
#include "Stack.h"
//default constructor
template <class T>
Stack<T>::Stack()
{
top = 0;
count = 0;
}
//accessor for the count member
template <class T>
int Stack<T>::get_count ()
{ return count; }
//method to determine if the list is empty
template <class T>
bool Stack<T>::isEmpty () const
{
if (count == 0)
return true;
else
return false;
}
// destructor
template <class T>
Stack<T>::~Stack()
{
while (!isEmpty())
pop();
}
//method to pop off the top item of the list,
//returning its value and removing it from the list
template <class T>
T Stack<T>::pop ()
{
if (isEmpty())
throw Underflow(); //if the stack is empty, throw underflow
node<T> *topHolder = top; //hold the top node in a pointer
top = top->next; //set the top to the next node in the stack
T storedValue = topHolder->info; //store the value of the old top node
delete topHolder; //delete the old top node
--count; //decrement the number of nodes in the stack
return storedValue; //return the value of the node that was popped
}
//method to return the data of the top node without
//removing it from the list
template <class T>
T Stack<T>::peek ()
{
if (isEmpty())
throw Underflow(); //if the stack is empty, throw underflow
return top->info; //return the info stored in the top node
}
//method to push a new node onto the top of the list
template <class T>
void Stack<T>::push (T a)
{
node newTop = new node(a); //allocate and construct the new node for the top of the stack
if (!isEmpty())
newTop.next = top->next; //set the next member of the new node to the current top node on the stack
top = *newTop; //set the top member of the stack to a pointer to the newTop
++count; //increment the number of nodes in the stack
}
Any help at all would be appreciated!
~SunnyD