2010-06-02 168 views
3

我努力使大量使用模板來封裝工廠類:C++模板功能 - >傳遞一個模板類作爲模板參數

的包裝類(即CLASSA)獲取被包裝類(即CLASSB )通過模板參數提供'可插入性'。

此外,我必須提供從包裝的內部類(innerB)繼承的內部類(innerA)。

的問題是G ++ 「gcc版本4.4.3(Ubuntu的4.4.3-4ubuntu5)」 的下面的錯誤消息:

[email protected]:~/Development/cppExercises/functionTemplate$ g++ -o test test.cpp 
test.cpp: In static member function ‘static classA<A>::innerA<iB>* classA<A>::createInnerAs(iB&) [with iB = int, A = classB]’: 
test.cpp:39: instantiated from here 
test.cpp:32: error: dependent-name ‘classA::innerA<>’ is parsed as a non-type, but instantiation yields a type 
test.cpp:32: note: say ‘typename classA::innerA<>’ if a type is meant 

正如你可以在方法createInnerBs的定義見,我打算傳遞一個非類型的參數。所以使用typename是錯誤的!

TEST.CPP的代碼如下:

class classB{ 
public: 
    template < class iB> 
    class innerB{ 
    iB& ib; 
    innerB(iB& b) 
     :ib(b){} 
    }; 

    template<template <class> class classShell, class iB> 
    static classShell<iB>* createInnerBs(iB& b){ 
    // this function creates instances of innerB and its subclasses, 
    // because B holds a certain allocator 

    return new classShell<iB>(b); 
    } 
}; 

template<class A> 
class classA{ 
    // intention of this class is meant to be a pluggable interface 
    // using templates for compile-time checking 
public: 
    template <class iB> 
    class innerA: A::template innerB<iB>{ 
    innerA(iB& b) 
     :A::template innerB<iB>(b){} 
    }; 

    template<class iB> 
    static inline innerA<iB>* createInnerAs(iB& b){ 
    return A::createInnerBs<classA<A>::template innerA<> >(b); // line 32: error occurs here 
    } 
}; 

typedef classA<classB> usable; 
int main (int argc, char* argv[]){ 
    int a = 5; 
    usable::innerA<int>* myVar = usable::createInnerAs(a); 

    return 0; 
} 

請幫助我,我一直在面對這個問題了好幾天。 這是不可能的,我想要做什麼?或者我忘了什麼?

感謝,塞瑪

回答

2

第32行應改爲:

return A::template createInnerBs<innerA>(b); 

因爲createInnerBs取決於模板參數A

您還需要製作公共構造函數innerAinnerB

+1

好的,謝謝,作爲我的經驗法則: 當涉及到模板時,總是使用typename/template指定嵌套名稱說明符中的類型或模板。 是否正確? – sema 2010-06-03 10:22:50

+0

是否有任何邏輯的原因,它是在C++中的'A :: template smth'而不是'template A :: smth'? – 2014-11-18 14:43:24

+1

@TigranSaluev:是的,你指定'smth'是一個模板,所以關鍵字必須與它關聯。否則,你將無法指定更多的嵌套模板,「A :: template B :: template C :: whatever' – 2014-11-18 16:35:40

1

以下是更正代碼編譯爲我:

class classB{ 
public: 
    template < class iB> 
    class innerB{ 
    iB& ib; 
    public: 
    innerB(iB& b) 
     :ib(b){} 
    }; 

    template<template <class> class classShell, class iB> 
    static classShell<iB>* createInnerBs(iB& b){ 
    // this function creates instances of innerB and its subclasses, 
    // because B holds a certain allocator 

    return new classShell<iB>(b); 
    } 
}; 

template<class A> 
class classA{ 
    // intention of this class is meant to be a pluggable interface 
    // using templates for compile-time checking 
public: 
    template <class iB> 
    class innerA: public A::template innerB<iB>{ 
    public: 
    innerA(iB& b) 
     : A::template innerB<iB>(b){} 
    }; 

    template<class iB> 
    static inline innerA<iB>* createInnerAs(iB& b); 
}; 

template<class A> 
template<class iB> 
inline classA<A>::innerA<iB>* classA<A>::createInnerAs(iB& b) 
{ 
    return A::template createInnerBs<classA::template innerA>(b); 
} 

typedef classA<classB> usable; 
int main (int argc, char* argv[]){ 
    int a = 5; 
    usable::innerA<int>* myVar = usable::createInnerAs(a); 

    return 0; 
} 

即使我認爲你過於複雜的事情......但我不完全理解你的使用情況。

相關問題