CodeGuru Forums -
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic Newsletters VB Forums Developer.com


Newest CodeGuru.com Articles:

  • ADO.NET Data Services in the .NET Framework
  • Visual C++ Programming: What's new for MFC library in VC++ 2010?
  • Microsoft Visual Studio LightSwitch and What It Can Do For You
  • Displaying Files and Folders in a GridView

  • Search CodeGuru:
     



    Go Back   CodeGuru Forums > Visual C++ & C++ Programming > Visual C++ Programming
    FAQ Members List Calendar Search Today's Posts Mark Forums Read

    Visual C++ Programming Ask questions about Windows programming with Visual C++ and help others by answering their questions.

    Reply
     
    Thread Tools Search this Thread Rate Thread Display Modes
      #1    
    Old February 18th, 2010, 05:25 PM
    HoldmysunnyD HoldmysunnyD is offline
    Junior Member
     
    Join Date: Feb 2010
    Posts: 3
    HoldmysunnyD is an unknown quantity at this point (<10)
    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
    Reply With Quote
      #2    
    Old February 19th, 2010, 10:27 AM
    D_Drmmr's Avatar
    D_Drmmr D_Drmmr is offline
    Member +
     
    Join Date: Jul 2005
    Location: Netherlands
    Posts: 828
    D_Drmmr  is a jewel in the rough (300+)D_Drmmr  is a jewel in the rough (300+)D_Drmmr  is a jewel in the rough (300+)D_Drmmr  is a jewel in the rough (300+)
    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
    Reply With Quote
    Reply

    Bookmarks
    Go Back   CodeGuru Forums > Visual C++ & C++ Programming > Visual C++ Programming


    Thread Tools Search this Thread
    Search this Thread:

    Advanced Search
    Display Modes Rate This Thread
    Rate This Thread:

    Posting Rules
    You may not post new threads
    You may not post replies
    You may not post attachments
    You may not edit your posts

    BB code is On
    Smilies are On
    [IMG] code is On
    HTML code is Off
    Forum Jump


    All times are GMT -5. The time now is 11:07 PM.



    Acceptable Use Policy

    Internet.com
    The Network for Technology Professionals

    Search:

    About Internet.com

    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers


    Powered by vBulletin® Version 3.7.3
    Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.