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:
Quote:
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();
}