模板类型输入用作成员属性 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 不是有效的模板构造函数内部。事实上,构造函数内的所有内容似乎都超出了范围并被标记,但我不明白为什么。非抽象版本确实还有许多其他成员变量和函数,但问题肯定出在外部类容器的构造函数中。
我哪里出错了/如何才能做得更好?
编辑:修复了一些编译器错误(我认为),我注意到这些错误与我在实际应用程序中没有制作的这个简单示例不同
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 actual application
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如@MSN 提到的,您需要使用嵌套模板。在您的情况下,它们采用以下形式:
并且它们的使用方式如下:
您提供的代码的真实示例(在 VC++ 2010 下编译):
As @MSN mentioned, you need to use nested templates. In your case they take the form of:
And they are used this way:
Real example from the code you have provided (compiles under VC++ 2010):
您想要将
FruitType
声明为 模板模板参数:You want to declare
FruitType
as a template template parameter: