Template sub-classing problem

This is a discussion on Template sub-classing problem within the C++ Programming forums, part of the General Programming Boards category; Forst of all here is my code Code: #include <list> #include <string> typedef std::string String; template<class DT> class State{ public: ...

  1. #1
    Registered User cfrost's Avatar
    Join Date
    Apr 2004
    Posts
    119

    Template sub-classing problem

    Forst of all here is my code

    Code:
    #include <list>
    #include <string>
    
    typedef std::string  String;
    
    template<class DT>
    class State{
        public:
            class Transition;
        public:
            State();
            State(const DT name);
            
            void setName(const DT name);
            const DT getName() const;
            
            bool addTransition(State<DT>::Transition);
            bool removeTransition(State<DT>::Transition);
        private:
            DT                            name;
    //		static Transition __useless__("USELESS");
    		std::list< State<DT>::Transition >	  mmap;
    /*
    	 *
    	 *Sub-class transition
    	 *
    	 */
    	public:
    	class Transition{
    		public:
    			Transition(const char *);
            
    			void setLabel(const char *label);
    			const char *getLabel() const;
            
    			void setTarget(const State<DT> *state);
    			const State<DT> *getTarget() const;
            
    			bool operator==(State<DT>::Transition);
    			bool operator<(State<DT>::Transition);
    		private:
    			String       label;
    			State<DT>    *next;
    	};
    };
    
    #endif
    Now when I instanciate a variable of
    State<unsigned int> st;

    Visual C gives me error
    Code:
    use of undefined type 'State<unsigned int>::Transition'; 
    at line std::list< State<DT>::Transition >	  mmap;
    what to do? Do any body knows how to use subclass instance?
    Last edited by cfrost; 02-20-2006 at 05:54 PM.
    Software is like sex it is good when it is free

  2. #2
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    you're using Transition before you define it. forward declarations can only be used for pointers or references. move the definition of Transition to where you forward declare it and it should work.

    dunno why you're forward declaring it there anyway...

    you do realise that you'll need to have the method implementations in the header for a template?
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600
    at the point where the list template is instantiated, Transition has not yet been defined. you can move the list declaration below Transition's definition and it should work fine.

    >> forward declarations can only be used for pointers or references

    are we talking about classes here or variables?

    >> you'll need to have the method implementations in the header for a template?

    you don't have to, it's just easier that way.
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  4. #4
    Registered User cfrost's Avatar
    Join Date
    Apr 2004
    Posts
    119
    Code:
    #include <list>
    #include <string>
    
    typedef std::string  String;
    
    template<class DT>
    class State{
        public:
    /*
     *
     *Sub-class transition
     *
     */
    public:
    class Transition{
    public:
    Transition(const char *);
            
    void setLabel(const char *label);
    const char *getLabel() const;
            
    void setTarget(const State<DT> *state);
    const State<DT> *getTarget() const;
            
    bool operator==(State<DT>::Transition);
    bool operator<(State<DT>::Transition);
    private:
    String       label;
    State<DT>    *next;
    };  
        public:
            State();
            State(const DT name);
            
            void setName(const DT name);
            const DT getName() const;
            
            bool addTransition(State<DT>::Transition);
            bool removeTransition(State<DT>::Transition);
        private:
            DT                            name;
    //static Transition __useless__("USELESS");
    std::list< State<DT>::Transition >  mmap;
    
    };
    
    template<class DT>
    State<DT>::State(){
    	return;
    }
    
    template<class DT>
    State<DT>::Transition::Transition(const char* nm){
    	label=nm;
    }
    
    void main(){
    	State<int> st();
    	State<int>::Transition tr("a");
    	return ;
    }
    here is portion of my code again now it says me
    C:\Documents and Settings\1860\Desktop\test.cpp(25) : error C2027: use of undefined type 'State<int>'
    C:\Documents and Settings\1860\Desktop\test.cpp(59) : see reference to class template instantiation 'State<int>' being compiled
    C:\Documents and Settings\1860\Desktop\test.cpp(26) : error C2027: use of undefined type 'State<int>'
    C:\Documents and Settings\1860\Desktop\test.cpp(59) : see reference to class template instantiation 'State<int>' being compiled
    C:\Documents and Settings\1860\Desktop\test.cpp(43) : error C2027: use of undefined type 'State<int>'
    C:\Documents and Settings\1860\Desktop\test.cpp(59) : see reference to class template instantiation 'State<int>' being compiled
    Error executing cl.exe.
    it is giving me error on
    bool operator==(State<DT>::Transition);
    inside Transition
    Software is like sex it is good when it is free

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,600
    what compiler are you using? - it compiles fine for me (other than the fact that only some of the methods are defined). main should return an int, by the way.
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. template overloading question
    By plutino in forum C++ Programming
    Replies: 14
    Last Post: 02-27-2009, 01:10 AM
  2. Default class template problem
    By Elysia in forum C++ Programming
    Replies: 5
    Last Post: 07-11-2008, 08:44 AM
  3. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  4. Linked list with two class types within template.
    By SilasP in forum C++ Programming
    Replies: 3
    Last Post: 02-09-2002, 05:13 AM
  5. Dynamic array classing problem
    By Zeeshan in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2001, 03:01 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21