我有一個Visual Studio 2008 C++應用程序,其中基類A_Base
需要實例化其類型由父類定義的數據成員。例如:使用由父類定義的類型的基類
template< typename T >
class A_Base
{
public:
typedef typename T::Foo Bar; // line 10
private:
Bar bar_;
};
class A : public A_Base<A>
{
public:
typedef int Foo;
};
int _tmain(int argc, _TCHAR* argv[])
{
A a;
return 0;
}
不幸的是,它出現在編譯器不知道T::Foo
是什麼,直到爲時已晚,我得到象這樣的錯誤:
1>MyApp.cpp(10) : error C2039: 'Foo' : is not a member of 'A'
1> MyApp.cpp(13) : see declaration of 'A'
1> MyApp.cpp(14) : see reference to class template instantiation 'A_Base<T>' being compiled
1> with
1> [
1> T=A
1> ]
1>MyApp.cpp(10) : error C2146: syntax error : missing ';' before identifier 'Bar'
1>MyApp.cpp(10) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>MyApp.cpp(10) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
有什麼辦法來實現這種類型的功能?
感謝, PaulH