这是一个例子。我在模板内部而非外部的 typename T::SubType*
上收到错误。
使用 gcc0x 我得到
prog.cpp: In instantiation of 'TemplateBase<A>':
prog.cpp:8:36: instantiated from here
prog.cpp:4:22: error: invalid use of incomplete type 'class A'
prog.cpp:8:7: error: forward declaration of 'class A'
在 vs 中,我收到一些随机错误消息。这是代码。
template< typename T >
struct TemplateBase {
void ff() {}
typename T::SubType*f(){ return 0; }
//typename T::SubType*f();
};
class A : public TemplateBase< A > {
public:
//struct SubTypeT {
struct SubType {
int i;
};
//typedef SubTypeT SubType;
private:
SubType m;
};
template<typename T>
typename T::SubType*f(){ return 0; }
int main() {
f<A>();
A a;
a.ff();
}
最佳答案
一个类在右大括号处完全定义,而不是在您列出其父类的位置。因此,在 class A : public TemplateBase< A > {
,你正在实例化 TemplateBase
具有不完整的 A 类。
关于c++ - 为什么我不能通过模板访问子类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6369595/