Thread: Problem in understanding the logic

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    222

    Problem in understanding the logic

    Hi,


    i have problem in understanding the logic behind function declaration and definitions which prevents me from writting the function outside the class scope:

    so this works

    Code:
    
    template <typename Tkey, typename Tvalue>
    class CPS_Index {
    
    private:
    	Tkey key;
    	Tvalue value;
    	
    	
    template<typename T1>
    void print(T1 x){
       cout << x;
    };
    
    public:
    
    	CPS_Index(Tkey k=0 ,Tvalue v=0);
    
    
    };
    
    
    template <typename Tkey, typename Tvalue>
    CPS_Index<Tkey,Tvalue>::CPS_Index(Tkey k ,Tvalue v) : key(k), value(v)
    {
    		print("Constructed!\n");
    }
    but i would like to write my function print() outside the class. something like :


    Code:
    
    
    template <typename Tkey, typename Tvalue>
    class CPS_Index {
    
    private:
    	Tkey key;
    	Tvalue value;
    	
    	template <typename T1>
            void print(T1 x);
    
    public:
    
    	CPS_Index(Tkey k=0 ,Tvalue v=0);
    
    
    };
    
    
    template <typename Tkey, typename Tvalue>
    CPS_Index<Tkey,Tvalue>::CPS_Index(Tkey k ,Tvalue v) : key(k), value(v)
    {
    		print("Constructed!\n");
    }
    
    template <typename Tkey, typename Tvalue>
    void CPS_Index<Tkey,Tvalue>::print( T1 x)
    {
    	cout << x;
    }
    This of course does not work since T1 is not recognized. How to make it work?

    thank you !

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The declaration involves two distinct template specifications, so the definition needs to as well.
    Code:
    template <typename Tkey, typename Tvalue>
    template<typename T1>
    void CPS_Index<Tkey,Tvalue>::print( T1 x)
    {
        cout << x;
    }
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sleep() function problem or logic problem?
    By FernandoBasso in forum C Programming
    Replies: 7
    Last Post: 11-16-2011, 05:50 PM
  2. Understanding some Logic
    By skg29 in forum C Programming
    Replies: 7
    Last Post: 10-09-2011, 07:44 AM
  3. i cant understanding this logic
    By nkrao123@gmail. in forum C Programming
    Replies: 6
    Last Post: 10-26-2009, 01:51 AM
  4. Understanding the pseudocode/logic for this program
    By Sholcomb1 in forum C Programming
    Replies: 11
    Last Post: 12-08-2006, 02:07 PM
  5. Re : Logic problem
    By Wah in forum C Programming
    Replies: 5
    Last Post: 01-31-2002, 02:19 PM