2012-05-18 84 views
0

对不起,这个问题似乎已被问了很多次,但我无法得到其他答案为我的设置工作。我有以下类和功能设置:如何将模板好友函数声明为模板类?

namespace ddd { 
    template <typename T> 
    class A { 
    ... 
    }; 

    template <typename T, typename U> 
    A<T> a_func(const A<U> &a) { 
    ... 
    } 
} 

我要声明a_func作为一个朋友,我想它,以便a_func是A的所有实例的朋友,无论哪个类型名用于T和U(例如,a_func可以访问A)。

谢谢!

+0

我看了你写的东西,但不知道你的意思*所有实例朋友什么* ...你想'a_func '成为'A '(可能还有'a_func ')成为'A '的朋友吗? –

+0

我想要a_func 成为A 的朋友。 – Max

回答

1

你可以做这样(看起来怎么样,你有话):

template<typename X> 
class A { 
    template<typename T, typename U> 
    friend A<T> a_func(const A<U>& a); 
}; 

template<typename T, typename U> 
A<T> a_func(const A<U>& a) { 
    // whatever 
} 

Demo

+0

@Max:你得到了什么错误?这应该工作(以及从模板的定义中删除'朋友'后...) –

+0

@DavidRodríguez-dribeas哈,固定谢谢。 –