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

发布于 2024-12-06 06:21:51 字数 1930 浏览 0 评论 0原文

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

我想要做的事情的抽象形式如下所示:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

糖粟与秋泊 2024-12-13 06:21:51

正如@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;
}

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;
}
夜血缘 2024-12-13 06:21:51

您想要将 FruitType 声明为 模板模板参数

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

You want to declare FruitType as a template template parameter:

template<..., template <typename, size_t> typename FruitType, ...>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文