[英]Template Type input used as Template Type for Member Property C++
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. 但是,目前,编译器告诉我FruitType不是构造函数内部的有效模板。 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 actual application 编辑:修复了一些编译器错误(我认为),我注意到该错误与我在实际应用程序中未做过的这个小例子不同
您想将FruitType
声明为模板template参数 :
template<..., template <typename, size_t> typename FruitType, ...>
As @MSN mentioned, you need to use nested templates. 如@MSN所述,您需要使用嵌套模板。 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): 您提供的代码的真实示例(在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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.