我有一个 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
有没有办法实现这种功能?
谢谢,保罗