Thread: Friends in a template class

  1. #1
    Registered User dalek's Avatar
    Join Date
    May 2003
    Posts
    135

    Friends in a template class

    Hi,

    I was just curious to see how I would implement a friend function in template class. It was purely as an excercise, as I have never really found a use for it in real life. Anyway, I came up with the following code. This works fine in MSVC 2003. However it doesn't compile in g++.

    You get the error:
    main.cpp:13: declaration of `class T'
    main.cpp:5: shadows template parm `class T'
    Does anyone know which one is correct?

    Code:
    #include <iostream>
    
    using namespace std;
    
    template <typename T>
    class CFoo
    {
    private:
    	T val;
    
    public:
    	CFoo(T d) : val(d) { }
    	template<typename T>
    	friend ostream & operator<<(ostream & os, const CFoo<T> & cf);
    };
    
    template <typename T>
    ostream & operator<<(ostream & os, const CFoo<T> & cf)
    {
    	os << cf.val << endl;
    
    	return os;
    }
    
    
    int main()
    {
    	CFoo<int> foo(10);
    
    	cout << foo << endl;
    	cin.get();
    }

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    This compiles for me:
    Code:
    #include <iostream>
    
    using namespace std;
    
    template <typename T>
    class CFoo
    {
    private:
    	T val;
    
    public:
    	CFoo(T d) : val(d) { }
    	//template<typename T>
    	friend ostream & operator<< <T>(ostream & os, const CFoo<T> & cf);
    };
    
    template <typename T>
    ostream & operator<<(ostream & os, const CFoo<T> & cf)
    {
    	os << cf.val << endl;
    
    	return os;
    }
    
    
    int main()
    {
    	CFoo<int> foo(10);
    
    	cout << foo << endl;
    	cin.get();
    }
    Edit: Here's a bunch of info on the subject (in chapter 16): http://oopweb.com/CPP/Documents/CPPA...cplusplus.html

    Particularly 16.2.5
    Last edited by JaWiB; 09-19-2004 at 07:57 PM.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Registered User dalek's Avatar
    Join Date
    May 2003
    Posts
    135
    Heh - how do you like that.

    Cheers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  3. template class default constructor problem
    By kocika73 in forum C++ Programming
    Replies: 3
    Last Post: 04-22-2006, 09:42 PM
  4. Declare a template class as a friend?
    By AH_Tze in forum C++ Programming
    Replies: 11
    Last Post: 05-19-2004, 09:24 PM
  5. Operator overloading in template classes
    By moejams in forum C++ Programming
    Replies: 5
    Last Post: 07-21-2003, 05:16 PM