This code:
- template<class T1> class C {
-
-
<code removed ...>
-
friend class A<T1>;
-
-
template<class T2> friend void B<T1,T2>::f1(void);
-
template<class T2> friend void B<T1,T2>::f2(void);
-
template<class T2> friend void B<T1,T2>::f3(void);
-
-
/*
-
* why is everything above allowed,
-
* but not the following line?
-
*/
-
-
template<class T2> friend class B<T1,T2>;
-
// error: partial specialization 'B<T1, T2>' declared 'friend'
-
-
};
-
In the above class template for C, type T1 is declared but suddenly a T2 appears. That makes this code not compilable:
- template<class T2> friend void B<T1,T2>::f3(void);
-
Change your class to:
- template<class T1, class T2> class C {
-
etc...
-
Then change the friend functions to:
- template<class T1, class T2> friend void B<T1,T2>::f1(void);
-
Finally, change your friend class to:
There will still be a problem with your friend member functions. In your C class template, the member functions are parameterized with T1 and T2. But there is no template for this function so the compiler doesn't now how to specialize it. You will need, in addition to the above changes, provide a function template for you class B member functions. I used:
- template<class T1, class T2> class B
-
{
-
public:
-
void f1(void);
-
};
-
-
template<class T1, class T2> void B<T1,T2>::f1(void) {}
-
When you use a function template inside another template, the inner template is either
bound or
unbound. It is bound when T1 and T2 are known types. In this case you can use a function prototype (as you did) since the template function already has been created. However, when T1 or T2 are not known, then you must supply the full template before the compiler sees the function prototype.