2010-06-14 71 views
1

可能重复:
problem with template inheritanceGCC模板问题

此代码不能在GCC编译:

template <typename T> 
struct Base 
{ 
public: 
int x; 
}; 

template <typename B> 
struct Middle : public B 
{ 
}; 

template <typename T> 
struct Sub : public Middle<Base<T> > 
{ 
public: 
void F() 
{ 
    x=1; // error: ‘x’ was not declared in this scope 
} 
}; 

如果任BaseSub没有模板类,它不会抱怨。 VC处理它。

为什么?

+0

[模板继承问题]的副本(http://stackoverflow.com/questions/2982660/problem-with-template-inheritance) – 2010-06-14 21:08:49

回答

4

使用this->x = 1;告诉编译器x是一个(模板)从属名称。 注意:根据标准GCC没有做得很好,MSVC只是更宽容一点。