Thread: Template sub-classing problem

  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 06: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,708
    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:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  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,708
    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:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

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, 02: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, 06:13 AM
  5. Dynamic array classing problem
    By Zeeshan in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2001, 04:01 PM