Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6620877
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:09:50+00:00 2026-05-25T21:09:50+00:00

So I am trying to have one template class be a container (that will

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T21:09:51+00:00Added an answer on May 25, 2026 at 9:09 pm

    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What i'm trying to do is create a template array class that will store
I am trying to learn more about regular expressions I have one below that
I have two classes one of which inherits from the other. Im trying to
Ok, so i have been trying to put everyone one of my classes in
I have a template class ArrayTemp that creates an array of pointers to type
I have a BinarySearchTree that consists of nodes which are both a template class
I'm trying to make a template class where one would be able to define
I am trying to have one one layer, and center images within. I.E., if
I'm trying to get rid of floats in my functions. I have one function
I am trying to make a Postgres PHP backup script. I have downloaded one

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.