c++ - 模板类的友元函数需要访问成员类型

标签 c++ templates friend

我需要为模板类定义一个友元函数。该功能有 作为类的成员类型的返回类型。现在,我不能事先声明它,因为当时不知道返回类型。像这样

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/

相关文章:

c++ - 为什么我们在 C++ 中使用 printf() 函数?

c++ - 基类模板的成员在具有相同模板参数的派生类模板中超出范围

c++ - 选择何时使用友元函数

c++ - 友元函数内部代码

C++:如何使用适当的 OO 有效地将 100 万的私有(private) vector 返回给其他几个类

c++ - 在鼠标事件期间更改 QWidget Parent

c++ - VS 2003 在后期构建期间无法注册 regsvr32。命令提示符没有问题

C++算法计算多个数字的最小公倍数

c++ - 如何使用访问者从变体返回特定类型?

C++11 std::tuple 到 std::array 转换导致可变参数模板崩溃