scalar deleting destructor linker issue

Written by kishore

I’m working on an assignment for my data structures class where we have to finish implementing a stack and node class from the professor’s header files. After writing the cpp’s for these classes, tried to declare an instance of the stack class in the main.cpp, but doing so causes VS to throw a linker error for “scalar deleting destructor”. I tried commenting out one/both of the destructors for the classes to see what was causing the problem, but the error persists. Does anyone know what is wrong? I’ve included the code below:

***********this is the Node.h file****************
template < class T>

class node
{
public:
T info; //member to store the data of the node

node* next; //member to hold pointer to next node

node(); //default constructor

node(T); //constructor with 1 parameter for data

~node(); //destructor
};
***********eof****************

***********this is the Node.cpp file****************
#include “Node.h”

//default constructor
template
node::node () :
info (new T),
next (NULL)
{}

//constructor with 1 parameter for data
template
node::node (T data)
{
info = new T data;
next = NULL;
}

// destructor
template
node::~node()
{ delete info; }
***********eof****************

***********this is the Stack.h file****************
#include “Node.h”

template < class T>

class Stack{

private:
node * 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
};

***********eof****************

***********this is the Stack.cpp file****************
#include “Stack.h”

//default constructor
template
Stack::Stack()
{
top = 0;
count = 0;
}

//accessor for the count member
template
int Stack::get_count ()
{ return count; }

//method to determine if the list is empty
template
bool Stack::isEmpty () const
{
if (count == 0)
return true;
else
return false;
}

// destructor
template
Stack::~Stack()
{
while (!isEmpty())
pop();
}

//method to pop off the top item of the list,
//returning its value and removing it from the list
template
T Stack::pop ()
{
if (isEmpty())
throw Underflow(); //if the stack is empty, throw underflow

node *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
T Stack::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
void Stack::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
}
***********eof****************

***********main.cpp****************
/*#include
#include
#include
using namespace std;
*/
//#include “Stack.h”
#include “Stack.cpp”

int main()
{
Stack s;

return 0;
}
***********eof***********

if anyone has any ideas I would be very grateful =)
~SunnyD

View post:
scalar deleting destructor linker issue

Feb
18

std::vector elements – heap or stack?

Written by kishore

Hello,

Suppose I create a std::vector object, containing a number of “MyClass” objects, such as:

Code:

Feb
18