| CodeGuru Home | VC++ / MFC / C++ | .NET / C# | Visual Basic | Newsletters | VB Forums | Developer.com |
|
|||||||
| Visual C++ Programming Ask questions about Windows programming with Visual C++ and help others by answering their questions. |
![]() |
|
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
scalar deleting destructor linker issue
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<T>* 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 <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; } ***********eof**************** ***********this is the Stack.h file**************** #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 }; ***********eof**************** ***********this is the Stack.cpp file**************** #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>: op (){ 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>: eek (){ 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>: ush (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 <iostream> #include <string> #include <cmath> using namespace std; */ //#include "Stack.h" #include "Stack.cpp" int main() { Stack<float> s; return 0; } ***********eof*********** if anyone has any ideas I would be very grateful =) ~SunnyD |
|
#2
|
||||
|
||||
|
Re: scalar deleting destructor linker issue
When you define a template class, you have to put the definition of the member functions in the header file (or a file included by the header file) and not in a cpp file. This is because the functions only get compiled when an instance of the template is created.
See this FAQ for more info.
__________________
Cheers, D Drmmr Please put [code][/code] tags around your code to preserve indentation and make it more readable. As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky |
![]() |
| Bookmarks |
|
||||||
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|