模板类型输入用作成员属性C ++的模板类型

时间:2011-09-23 21:58:00

标签: c++ class templates nested

所以我试图让一个模板类成为一个容器(稍后将对其进行操作)一组包含的类,也是从模板生成的,并存储在向量中。

我想要做的抽象形式看起来像这样:

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不是构造函数内的有效模板。事实上,构造函数中的所有内容似乎都超出范围并被标记,但我无法弄清楚原因。 unabstracted版本也有许多其他成员变量和函数,但问题肯定在外部类容器的构造函数中。

我哪里出错/怎么可以做得更好?

编辑:修复了一些编译器错误(我认为),我注意到这些错误与我在实际应用程序中没有做的这个简单的例子不同

2 个答案:

答案 0 :(得分:0)

您想将FruitType声明为template template parameter

template<..., template <typename, size_t> typename FruitType, ...>

答案 1 :(得分: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;
}