Use a template with a class

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sake
    New Member
    • Jan 2007
    • 46

    Use a template with a class

    Hi,
    I recently came across a problem in my C++ adventures. I was making a function class that happened to use classes. It also needed to be flexible for any variable type. So without hesitance, I used a template. Happily, I continued the program, until I began to declare the list. Here was my code:
    Code:
    template <class T>
    
    class Stack {
    	public:  
    		virtual void push(T c) = 0;
    		virtual char pop() = 0;
    };
    
    template <class T>
    
    class List_Stack : public Stack {
          
    	private:
    		list <T> lc;
    };
    As we can see, this snippet has some problems, and a question arises because of that problem. How can I use a template with the list class? So that list will work for any variable type.

    Also, the compiler keeps complaining this:
    Code:
    24 program.cpp expected class-name before '{' token
    Line 24 refers to:
    Code:
    class List_Stack : public Stack {
    Any help?

    Thanks,
    Austen
  • Prasannaa
    New Member
    • May 2007
    • 21

    #2
    Hi,

    Since Stack is a template class it needs a Template Argument.
    Second, list and std namespace has to included.
    class List_Stack : public Stack<T>
    Code:
    #include<list>
    //using namespace std;
    
    template <class T>
    class Stack {
    	public:  
    		virtual void push(T c) = 0;
    		virtual char pop() = 0;
    };
    
    template <class T>
    class List_Stack : public Stack<T> {
          
    	private:
    		std::list<T> lc;
    };
    Thanks,
    Prasannaa

    Comment

    Working...