views:

85

answers:

1

My question is related to this question.

#include<iostream>
template< typename T >
class T1 {
public:
    T i;
    void display()
    {
       std::cout<<i<<"\n"<<j<<"\n"<<k; 
 }
protected:
    T j;
private:
    T k;
    friend void Test( T1 &obj);
};

template<typename T>
void Test(T1<T> &obj)
{
    T a=T();

    obj.i=a;
    obj.j=a;
    obj.k=a;
}


int main()
{
   T1<int>a;
   Test(a);
   a.display();
}

Why doesn't the above code compile?

+10  A: 

friend void Test( T1 &obj); declares a non template function.

Declare it as a template.

Try this :

....
private:
T k;
template<typename U>
  friend void Test( T1<U> &obj);
};
Prasoon Saurav