我正在尝试定义仅包含 typedef 的基类。
template<typename T>
class A
{
public:
typedef std::vector<T> Vec_t;
};
template<typename T>
class B : public A<T>
{
private:
Vec_t v; // fails - Vec_t is not recognized
};
为什么在 B 中我收到 Vec_t 无法识别的错误,我需要明确地写它?
typename A<T>::Vec_t v;
最佳答案
我相信这个问题是重复的,但我现在找不到了。 C++ 标准规定您应该根据 14.6.2/3 完全限定名称:
In the definition of a class template or a member of a class template, if a base class of the class template depends on a template-parameter, the base class scope is not examined during unqualified name lookup either at the point of definition of the class template or member or during an instantiation of the class template or member.
UPD: 我终于找到了重复项:here it is .
关于c++ - 将 'typedef' 从基类传播到 'template' 的派生类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23221841/