2011-09-23 85 views
0

所以我想讓一個模板類成爲一個容器(稍後將操作)一組包含的類,它們也是從模板生成的,並存儲在向量中。用作成員屬性C++的模板類型的模板類型輸入

的什麼,我試圖做的是這樣的抽象形式:

template <typename T, size_t numberofapples> 
class Apples { 

    public: 
     Apples(std::vector<T> appleinfo1, std::vector<T> appleinfo2); 

    protected: 
     std::vector<T> apple_stats; 
     std::vector<T> info1, info2; 


}; 

template <typename T, size_t numberofapples> 
Apples<T, numberofapples>::Apples(std::vector<T> appleinfo1, std::vector<T> appleinfo2) : apple_stats(numberofapples, 0){ 
    for (size_t i = 0; i < numberofapples; ++i) { 
     apple_stats[i] = rand(); 
    } 

    info1 = appleinfo1; 
    info2 = appleinfo2; 


} 



template <typename T, typename FruitType, size_t numberoffruitperbranch> 
class Tree { 

    public: 
     Tree(size_t numberofbranches, std::vector<T> commonfruitinfo1, std::vector<T> commonfruitinfo2); 

    protected: 
     std::vector<FruitType<T, numberoffruitperbranch> > branchset; 

};  

template <typename T, typename FruitType, size_t numberoffruitperbranch> 
Tree<T, FruitType, numberoffruitperbranch>::Tree(size_t numberofbranches, std::vector<T> commonfruitinfo1, std::vector<T> commonfruitinfo2) : { 

    typename FruitType<T, numberoffruitperbranch> single_fruit(fruitinfo1, fruitinfo2); 

    branchset.resize(numberofbranches, single_fruit); 
    //in the un-abstracted version that has nothing to do with fruit, I'd then iterate over the vector and run some internal code on each one 
} 

的目標是,我希望能夠做這樣的事情:

Tree<double, Apples, 10> MyFirstTree(5, vectorofdata, secondvectorofdata); 

但是,目前,編譯器告訴我FruitType在構造函數中不是一個有效的模板。實際上,構造函數中的所有內容似乎都超出了範圍,並且被標記,但我無法弄清楚原因。未被引用的版本也有許多其他成員變量和函數,但問題肯定在外部類容器的構造函數中。

我哪裏去錯了/怎麼能做得更好?

編輯:修正了一些編譯器錯誤(我認爲),我注意到了從這個簡單的例子不同,我並沒有在實際應用中

+1

此代碼充斥着編譯器錯誤。我只能從'typename FruitType '和'std :: vector branchset;'''你試圖使用相同的參數作爲類型(例如'Apples ')並且作爲模板(只是' Apples')。你需要下定決心。 – UncleBens

+0

我不跟着你,這可能是問題所在。最終我有2個班,'蘋果'和'樹'。蘋果包含類型,(在這個例子中是double),樹最終應該包含蘋果(在本例中爲double)。在所有情況下,蘋果都應該是一個由模板生成的類型。這不可能嗎?該向量應該像std :: vector >? – Avacar

+0

除了@UncleBens註釋 - 'class'不是'C++''''std :: vector',而不是'std:vector',''是'};''不是'}'的末端類,可能有更多... –

回答

0

正如@MSN提到的,你需要使用嵌套模板。在你的情況下,他們採取的形式是:

template<typename T, size_t nr, template <typename, size_t> class FruitType> 
class Tree { ... }; 

而且他們用這樣的方式:

Tree<double, 20, Apple> someTree; 

從您提供的代碼真實的例子(VC下編譯++ 2010):

#include <iostream> 
#include <vector> 

template <typename T, size_t numberofapples> 
class Apples { 

    public: 
     Apples(std::vector<T> appleinfo1, std::vector<T> appleinfo2); 

    protected: 
     std::vector<T> apple_stats; 
     std::vector<T> info1, info2; 


}; 

template <typename T, size_t numberofapples> 
Apples<T, numberofapples>::Apples(std::vector<T> appleinfo1, std::vector<T> appleinfo2) : apple_stats(numberofapples, 0){ 
    for (size_t i = 0; i < numberofapples; ++i) { 
     apple_stats[i] = rand(); 
    } 

    info1 = appleinfo1; 
    info2 = appleinfo2; 


} 



template <typename T, size_t numberoffruitperbranch, template <typename, size_t> class FruitType> 
class Tree { 

    public: 
     Tree(size_t numberofbranches, std::vector<T> commonfruitinfo1, std::vector<T> commonfruitinfo2); 

    protected: 
     std::vector<FruitType<T, numberoffruitperbranch> > branchset; 

};  

template <typename T, size_t numberoffruitperbranch, template <typename, size_t> class FruitType> 
Tree<T, numberoffruitperbranch, FruitType>::Tree(size_t numberofbranches, std::vector<T> commonfruitinfo1, std::vector<T> commonfruitinfo2) { 

    typename FruitType<T, numberoffruitperbranch> single_fruit(commonfruitinfo1, commonfruitinfo2); 

    branchset.resize(numberofbranches, single_fruit); 
    //in the un-abstracted version that has nothing to do with fruit, I'd then iterate over the vector and run some internal code on each one 
}; 

int main() 
{ 
    Tree<double, 10, Apples> someTree(20, std::vector<double>(), std::vector<double>()); 
    return 0; 
}