2010-06-14 118 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只是更寬容一點。