开发者

Template Type input used as Template Type for Member Property C++

开发者 https://www.devze.com 2023-04-08 14:33 出处:网络
So I am trying to have one template class be a container (that will later operate on) a set of contained classes, also generated from a template, and stored in a vector.

So I am trying to have one template class be a container (that will later operate on) a set of contained classes, also generated from a template, and stored in a vector.

The abstracted form of what I'm trying to do would look like this:

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
}

The goal is that I'd like to be able to do something like:

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

At the moment, however, the compiler is telling me that FruitType is not a valid template inside the constructor function. In fact, everything inside the constructor appears to be out of scope and is being flagged, but I can't figure out why. The unabstracted version also does have a number of other member variables and functions, but the problem is definitely in the constructor of the outer class container.

Where am I going wrong/how could this be done better?

edit: fixed some compiler errors (I think) which I noticed were different from this trivial example that I did not make in the a开发者_高级运维ctual application


As @MSN mentioned, you need to use nested templates. In your case they take the form of:

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

And they are used this way:

Tree<double, 20, Apple> someTree;

Real example from the code you have provided (compiles under 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;
}


You want to declare FruitType as a template template parameter:

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

精彩评论

暂无评论...
验证码 换一张
取 消