我需要为模板类定义一个友元函数。该功能有 作为类的成员类型的返回类型。现在,我不能事先声明它,因为当时不知道返回类型。像这样
template<class T> class A;
//This doesn't work: error: need ‘typename’ before...
template<class T> A<T>::member_type fcn(A<T>::member_type);
//This doesn't work: error: template declaration of ‘typename...
template<class T> typename A<T>::member_type fcn(A<T>::member_type);
template<class T>
class A{
public:
typedef int member_type;
friend member_type fcn<T>(member_type);
};
我该怎么做?
最佳答案
我设法使用 g++ 在 g++ 上编译该代码:
template<class T> typename A<T>::member_type fcn(typename A<T>::member_type);
(因此需要第二个“typename”)
关于c++ - 模板类的友元函数需要访问成员类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12963239/