[英]Template Type input used as Template Type for Member Property 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不是構造函數內部的有效模板。 實際上,構造函數中的所有內容似乎都超出范圍並被標記,但我不知道為什么。 完整版本也確實具有許多其他成員變量和函數,但問題肯定出在外部類容器的構造函數中。
我哪里出問題了/如何才能更好地做到這一點?
編輯:修復了一些編譯器錯誤(我認為),我注意到該錯誤與我在實際應用程序中未做過的這個小例子不同
您想將FruitType
聲明為模板template參數 :
template<..., template <typename, size_t> typename FruitType, ...>
如@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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.