Thread: trying to implement stack using class template

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    77

    trying to implement stack using class template

    I am getting errors...please rectify my code or atleast point out where I am wrong

    Code:
    #include <iostream>#include <conio.h>
    
    
    using namespace std;
    
    
    template <class T>
    class NODE
    {
          friend class STACK;
          private:
                  
          T DATA;
          NODE<T> *LINK;
           
          };
          
     template <class T>     
     class STACK
     {
           private:               
                  NODE<T> *TOP;
           public:
                  STACK(){TOP=NULL;}
                  STACK<T> &PUSH(T x);
                  STACK<T> &POP(T *x);
                  void Display();      
           
           };
    
    
     template <class T>      
    STACK<T> &STACK<T>::PUSH(T x)
    {
          NODE<T> *NEW=new NODE<T>;
          NEW->DATA=x;
          NEW->LINK=TOP;
          TOP=NEW;      
          return *this;
          
          }
    
    
    template <class T>      
    STACK<T> &STACK<T>::POP(T *x)
    {
          NODE<T> *DEL=TOP;
          *x=DEL->DATA;
          TOP=TOP->LINK;
          delete DEL;
          return *this;
          }
          
    template <class T>      
    void STACK<T>::Display()
    {
         NODE<T> *PTR=TOP;
         while(PTR!=NULL)
                         {
                         cout<<PTR->DATA<<endl;
                         PTR=PTR->LINK;                
                         }
         }
         
         
    
    
    
    
    int main()
        {
        STACK<int> S;
        int s;
        S.PUSH(45);
        S.PUSH(76);
        S.PUSH(44);
        S.PUSH(23);
        S.POP(&s);
        cout<<"popped data : "<<s<<endl;        
        S.Display();    
        getch();
        return 0;
             }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I guess you have the title right this time. I'll repost:
    "If you are getting errors, you could at least bother to say what they are in the post.
    (EDIT: Although I do note that you don't bother to check whether anything exists on the stack before you (try to) pop a value off.)"

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Have you tried implementing your last two class templates as non-templates, e.g., assume that T is int and simply don't involve templates until your code is correct.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Aug 2012
    Posts
    77
    yes I did heres the code

    Code:
    #include <iostream>#include <conio.h>
    
    
    using namespace std;
    
    
    class NODE
    {
          friend class STACK;
          private:
                  
          int DATA;
          NODE *LINK;
           
          };
     class STACK
     {
           private:               
                  NODE *TOP;
           public:
                  STACK(){TOP=NULL;}
                  STACK &PUSH(int x);
                  STACK &POP(int *x);
                  void Display();      
           
           };
           
    STACK& STACK::PUSH(int x)
    {
          NODE *NEW=new NODE;
          NEW->DATA=x;
          NEW->LINK=TOP;
          TOP=NEW;      
          return *this;
          
          }
          
    STACK &STACK::POP(int *x)
    {
          NODE *DEL=TOP;
          *x=DEL->DATA;
          TOP=TOP->LINK;
          delete DEL;
          return *this;
          }
    void STACK::Display()
    {
         NODE *PTR=TOP;
         while(PTR!=NULL)
                         {
                         cout<<PTR->DATA<<endl;
                         PTR=PTR->LINK;                
                         }
         
         }
         
         
    int main()
        {
        STACK S;
        int s;
        S.PUSH(45);
        S.PUSH(76);
        S.PUSH(44);
        S.PUSH(23);
        S.POP(&s);
        cout<<"popped data : "<<s<<endl;        
        S.Display();    
        
        getch();
        return 0;
             }

  5. #5
    Registered User
    Join Date
    Aug 2012
    Posts
    77
    Quote Originally Posted by tabstop View Post
    I guess you have the title right this time. I'll repost:
    "If you are getting errors, you could at least bother to say what they are in the post.
    (EDIT: Although I do note that you don't bother to check whether anything exists on the stack before you (try to) pop a value off.)"
    just trying a straightforward implementation...later Ill modify my code as per my requirement thank you

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    So, does your code work? If not, how does it not work?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Aug 2012
    Posts
    77
    Quote Originally Posted by laserlight View Post
    So, does your code work? If not, how does it not work?
    It didnt work.........

    errors:

    In instantiation of `NODE<int>':
    34 instantiated from `STACK<T>& STACK<T>::PUSH(T) [with T = int]'
    66 instantiated from here
    8 template argument required for `struct STACK'
    12 `int NODE<int>:: DATA' is private

    and more....

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So when we say post the errors, why not ... post the errors?
    Code:
    tabstop@ubuntu:~/helping$ g++ -Wall -o stack stack.cpp
    stack.cpp: In function ‘int main()’:
    stack.cpp:81:11: error: ‘getch’ was not declared in this scope
    stack.cpp: At global scope:
    stack.cpp: In instantiation of ‘NODE<int>’:
    stack.cpp:35:22:   instantiated from ‘STACK<T>& STACK<T>::PUSH(T) [with T = int]’
    stack.cpp:74:14:   instantiated from here
    stack.cpp:9:1: error: template argument required for ‘struct STACK’
    stack.cpp: In member function ‘STACK<T>& STACK<T>::PUSH(T) [with T = int]’:
    stack.cpp:74:14:   instantiated from here
    stack.cpp:13:7: error: ‘int NODE<int>::DATA’ is private
    stack.cpp:36:5: error: within this context
    stack.cpp:14:14: error: ‘NODE<int>* NODE<int>::LINK’ is private
    stack.cpp:37:5: error: within this context
    stack.cpp: In member function ‘STACK<T>& STACK<T>::POP(T*) [with T = int]’:
    stack.cpp:78:13:   instantiated from here
    stack.cpp:13:7: error: ‘int NODE<int>::DATA’ is private
    stack.cpp:48:5: error: within this context
    stack.cpp:14:14: error: ‘NODE<int>* NODE<int>::LINK’ is private
    stack.cpp:49:5: error: within this context
    stack.cpp: In member function ‘void STACK<T>::Display() [with T = int]’:
    stack.cpp:80:15:   instantiated from here
    stack.cpp:13:7: error: ‘int NODE<int>::DATA’ is private
    stack.cpp:60:9: error: within this context
    stack.cpp:14:14: error: ‘NODE<int>* NODE<int>::LINK’ is private
    stack.cpp:61:9: error: within this context
    The big one is probably this one:
    stack.cpp:9:1: error: template argument required for ‘struct STACK’
    as you have tried to declare STACK (by itself) as a friend of NODE<T>. You can forward declare the STACK class and then declare STACK<T> as a friend. (The "right" way to do it is for the NODE class to provide methods to access and set those variables, of course.)

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    By the way, stop fully capitalising your class and member names. Fully capitalised names are conventionally reserved for macros and constants.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Aug 2012
    Posts
    77
    Quote Originally Posted by tabstop View Post
    So when we say post the errors, why not ... post the errors?
    Code:
    tabstop@ubuntu:~/helping$ g++ -Wall -o stack stack.cpp
    stack.cpp: In function ‘int main()’:
    stack.cpp:81:11: error: ‘getch’ was not declared in this scope
    stack.cpp: At global scope:
    stack.cpp: In instantiation of ‘NODE<int>’:
    stack.cpp:35:22:   instantiated from ‘STACK<T>& STACK<T>::PUSH(T) [with T = int]’
    stack.cpp:74:14:   instantiated from here
    stack.cpp:9:1: error: template argument required for ‘struct STACK’
    stack.cpp: In member function ‘STACK<T>& STACK<T>::PUSH(T) [with T = int]’:
    stack.cpp:74:14:   instantiated from here
    stack.cpp:13:7: error: ‘int NODE<int>::DATA’ is private
    stack.cpp:36:5: error: within this context
    stack.cpp:14:14: error: ‘NODE<int>* NODE<int>::LINK’ is private
    stack.cpp:37:5: error: within this context
    stack.cpp: In member function ‘STACK<T>& STACK<T>::POP(T*) [with T = int]’:
    stack.cpp:78:13:   instantiated from here
    stack.cpp:13:7: error: ‘int NODE<int>::DATA’ is private
    stack.cpp:48:5: error: within this context
    stack.cpp:14:14: error: ‘NODE<int>* NODE<int>::LINK’ is private
    stack.cpp:49:5: error: within this context
    stack.cpp: In member function ‘void STACK<T>::Display() [with T = int]’:
    stack.cpp:80:15:   instantiated from here
    stack.cpp:13:7: error: ‘int NODE<int>::DATA’ is private
    stack.cpp:60:9: error: within this context
    stack.cpp:14:14: error: ‘NODE<int>* NODE<int>::LINK’ is private
    stack.cpp:61:9: error: within this context
    The big one is probably this one:
    stack.cpp:9:1: error: template argument required for ‘struct STACK’
    as you have tried to declare STACK (by itself) as a friend of NODE<T>. You can forward declare the STACK class and then declare STACK<T> as a friend. (The "right" way to do it is for the NODE class to provide methods to access and set those variables, of course.)
    I would be greatful to you if you ellaborate...I am new to c++.. a block of code as an example will help

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Remember this is just a hack until you write a proper node class.

    Code:
    template <class T> class STACK; //forward declare STACK as a templated class
    
    /* ... then in your NODE class ... */
    friend class STACK<T>;

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by progmateur
    I would be greatful to you if you ellaborate...I am new to c++.. a block of code as an example will help
    For starters, you need to rename what I suggested and format your code properly, e.g.,
    Code:
    #include <iostream>
    
    template <class T>
    class Stack;
    
    template <class T>
    class Node
    {
        friend class Stack<T>;
    private:
    
        T data;
        Node<T>* link;
    };
    
    template <class T>
    class Stack
    {
    private:
        Node<T>* top;
    public:
        Stack() : top(NULL) {}
        Stack<T>& push(T x);
        Stack<T>& pop(T* x);
        void display(std::ostream& out);
    };
    
    template <class T>
    Stack<T>& Stack<T>::push(T x)
    {
        Node<T>* new_node = new Node<T>;
        new_node->data = x;
        new_node->link = top;
        top = new_node;
        return *this;
    }
    
    template <class T>
    Stack<T>& Stack<T>::pop(T* x)
    {
        Node<T>* delete_node = top;
        *x = delete_node->data;
        top = top->link;
        delete delete_node;
        return *this;
    }
    
    template <class T>
    void Stack<T>::display(std::ostream& out)
    {
        Node<T>* node = top;
        while (node != NULL)
        {
            out << node->data << std::endl;
            node = node->link;
        }
    }
    
    int main()
    {
        using namespace std;
    
        Stack<int> stack_obj;
        int value;
        stack_obj.push(45);
        stack_obj.push(76);
        stack_obj.push(44);
        stack_obj.push(23);
        stack_obj.pop(&value);
        cout << "popped data : " << value <<endl;
        stack_obj.display(cout);
        return 0;
    }
    I have taken the liberty of adding the fix that you're looking for, i.e., the forward declaration of the Stack class template and then the change in the friend class declaration. Furthermore, I have changed the display member function to take an argument, and moved the using directive to within the main function: you shouldn't have a using directive at file scope before a class definition as the class definition will almost certainly be moved to a header file if you do ever have one.

    By the way, have you ever seen the std::stack container adapter interface?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Aug 2012
    Posts
    77
    Quote Originally Posted by laserlight View Post
    For starters, you need to rename what I suggested and format your code properly, e.g.,
    Code:
    #include <iostream>
    
    template <class T>
    class Stack;
    
    template <class T>
    class Node
    {
        friend class Stack<T>;
    private:
    
        T data;
        Node<T>* link;
    };
    
    template <class T>
    class Stack
    {
    private:
        Node<T>* top;
    public:
        Stack() : top(NULL) {}
        Stack<T>& push(T x);
        Stack<T>& pop(T* x);
        void display(std::ostream& out);
    };
    
    template <class T>
    Stack<T>& Stack<T>::push(T x)
    {
        Node<T>* new_node = new Node<T>;
        new_node->data = x;
        new_node->link = top;
        top = new_node;
        return *this;
    }
    
    template <class T>
    Stack<T>& Stack<T>::pop(T* x)
    {
        Node<T>* delete_node = top;
        *x = delete_node->data;
        top = top->link;
        delete delete_node;
        return *this;
    }
    
    template <class T>
    void Stack<T>::display(std::ostream& out)
    {
        Node<T>* node = top;
        while (node != NULL)
        {
            out << node->data << std::endl;
            node = node->link;
        }
    }
    
    int main()
    {
        using namespace std;
    
        Stack<int> stack_obj;
        int value;
        stack_obj.push(45);
        stack_obj.push(76);
        stack_obj.push(44);
        stack_obj.push(23);
        stack_obj.pop(&value);
        cout << "popped data : " << value <<endl;
        stack_obj.display(cout);
        return 0;
    }
    I have taken the liberty of adding the fix that you're looking for, i.e., the forward declaration of the Stack class template and then the change in the friend class declaration. Furthermore, I have changed the display member function to take an argument, and moved the using directive to within the main function: you shouldn't have a using directive at file scope before a class definition as the class definition will almost certainly be moved to a header file if you do ever have one.

    By the way, have you ever seen the std::stack container adapter interface?
    very kind of you...now I understand the meaning of forward declaration!

  14. #14
    Registered User
    Join Date
    Jul 2013
    Location
    Germany
    Posts
    499

    Question is there an advantage to this?

    I really like how this site allows us new guys to read so much code and see how others think. However I was wondering what is the advantage to writing it like this then lets say using one Class and one Struct which seems easier to read.

    Just was wondering..

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by jocdrew21 View Post
    I really like how this site allows us new guys to read so much code and see how others think. However I was wondering what is the advantage to writing it like this then lets say using one Class and one Struct which seems easier to read.

    Just was wondering..
    A struct is a class (more or less, with some restrictions), so there isn't a difference, let alone an advantage.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. trying to implement linkedlist using class template
    By progmateur in forum C++ Programming
    Replies: 1
    Last Post: 12-05-2013, 08:34 AM
  2. how can implement a template class like this
    By letmoon in forum C++ Programming
    Replies: 17
    Last Post: 03-26-2012, 01:00 PM
  3. Replies: 27
    Last Post: 08-21-2008, 11:38 AM
  4. Replies: 9
    Last Post: 11-12-2007, 03:29 PM