问题描述
我该如何解决这个错误?
How can I solve this error?
我的头文件
template<typename T>
class C1 {
public:
typedef std::vector<T::F> TFV;
TFV Function1();
};
我的 CPP 文件
template<typename T>
TFV C1::Function() //error: ‘TFV’ does not name a type
{ }
推荐答案
首先,使用 typename
关键字告诉编译器将 F
解释为 (qualified) 类型名称:
First of all, use the typename
keyword to tell the compiler to interpret F
as the (qualified) name of a type:
typedef std::vector<typename T::F> TFV;
// ^^^^^^^^
其次,TFV
不是在全局命名空间中定义的类型,因此您必须在 Function1()
的定义中正确地限定它:
Secondly, TFV
is not a type defined in the global namespace, so you have to properly qualify that as well in the definition of Function1()
:
template<typename T>
typename C1<T>::TFV C1<T>::Function1()
// ^^^^^^^^ ^^^^^^^ ^^^
{ }
最后,类模板的成员函数的定义应该放在头文件中,除非您为所有模板实例提供显式实例,否则会隐式生成.
Finally, the definition of member functions of a class template should be placed in a header file, unless you are providing explicit instantiations for all the template instantiations you would otherwise implicitly generate.
如果不这样做,很可能会导致链接器出现未解决的引用错误.
Failing to do so will most likely result in an unresolved references error from the linker.
这篇关于C++中带有模板参数的Typedef的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!