我試圖使用一個簡單的CRTP形式(奇怪的循環模板模式),因爲我有幾個類,每個類都有幾個相關的類,我想要一種綁定方式他們在一起(例如,我有像Widget,Doobry和Whatsit這樣的類,以及相關的類WidgetHandle,DoobryHandle和WhatsitHandle)。編譯與依賴類型的簡單CRTP案件的錯誤
我用來繼承Base
的每個類都添加了一個value_type
typedef,以便基類可以將其引用爲typename TWrapper::value_type
。
struct WidgetHandle {};
template <typename TWrapper>
class Base
{
public:
Base(typename TWrapper::value_type value_)
: value(value_) {}
typename TWrapper::value_type value;
};
class Widget : public Base<Widget>
{
public:
typedef WidgetHandle value_type;
Widget(WidgetHandle value_) : Base<Widget>(value_) {}
};
int main(int argc, char* argv[])
{
Widget i(WidgetHandle());
return 0;
}
但是,我得到的編譯錯誤:
scratch1.cpp(10): error C2039: 'value_type' : is not a member of 'Widget'
scratch1.cpp(16) : see declaration of 'Widget'
scratch1.cpp : see reference to class template instantiation 'Base<TWrapper>' being compiled
1> with
1> [
1> TWrapper=Widget
1> ]
scratch1.cpp(10): error C2039: 'value_type' : is not a member of 'Widget'
這是VS2010,雖然我得到類似的錯誤鏗鏘。我在這裏錯過了什麼?
將Widget作爲參數傳遞給'Base'時,'Widget'是不完整的類型。 – jrok
實際上,我沒有在任何地方看到'value_type'。 – lapk
@PetrBudnik它是在'Widget'主體的開始處。 – jrok