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 6892511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:32:39+00:00 2026-05-27T06:32:39+00:00

I have created N classes that take from one to N ints into constructors

  • 0

I have created N classes that take from one to N ints into constructors (and I hoped that would be enough) but appeared I am wrong.

Here are 2 sample classes:

template < class T0 , class T1>
class my_map {
    typedef T0 type_0;
    typedef T1 type_1;
    std::map<std::string, type_0* > T0_var;
    std::map<std::string, type_1* > T1_var;
    friend class apitemp;
public:
    my_map(  int meaningless0 = 42  , int meaningless1 = 42  ) {}
    class apitemp {
        std::string n_;
        my_map* p;
    public: apitemp(std::string name_, my_map* parent) : n_(name_), p(parent) {}
            operator type_0*() {return p->T0_var[n_] ; }
            operator type_1*() {return p->T1_var[n_] ; } 
    };
    void insert(std::string name, type_0* ptr)
    { T0_var[name] = ptr; } 
    void insert(std::string name, type_1* ptr)
    { T1_var[name] = ptr; } 
    apitemp operator[](std::string n_) {return apitemp(n_, this);} 
}; 

template < class T0> 
class my_map
{ 
    typedef T0 type_0; 
    std::map<std::string, type_0* > T0_var;
    friend class apitemp;
public: 
    my_map(  int meaningless0 = 42  ) {} 
    class apitemp 
    {
        std::string n_;
        my_map* p;
    public:
        apitemp(std::string name_, my_map* parent) : n_(name_), p(parent) {}
        operator type_0*() {return p->T0_var[n_] ; }
    }; 
    void insert(std::string name, type_0* ptr) 
    { T0_var[name] = ptr; } 
    apitemp operator[](std::string n_) {return apitemp(n_, this);} 
};

Order does not matter… when both classes are present I can’t compile my code, when one is commented (and we use API from only one of 2 classes) everything compiles… but when I try to use both I get compiler errors… So I wonder how to make such classes overridable?

  • 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-27T06:32:40+00:00Added an answer on May 27, 2026 at 6:32 am

    You cannot “overload” a template on the number of type parameters like you are trying to do. The correct reaction to this restriction depends on what exactly you are trying to do.

    It seems that your purpose here is to make a map that maps from a string to a variable number of values (set at compile time). But you already have such a templated container: std::map itself!

    // This maps to int
    std::map<std::string, int> map_to_int;
    
    struct foo {
        std::string str;
        int i;
    };
    
    // This effectively maps to 2 fields
    std::map<std::string, struct foo> map_to_struct;
    
    // etc etc
    

    If you were allowed to write

    std::map<std::string, std::string*, int*> does_not_compile;
    

    that would not give you anything that the map_to_struct above does not already give you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a collection of classes that inherit from an abstract class I created.
I have several classes, that all derives from SuperClass. When the classes are created,
Basically, I have a model where I've created a superclass that many other classes
I have three classes that all have a static function called 'create'. I would
I have authored some custom classes that I would like to create using XAML:
I have a table, say STUDENTS, that is related one-to-many to another table, CLASSES.
I have a script that parse the database and create php classes to work
I have a XML schema that I will need to create Java classes for.
I have this class that have a function to load other classes and create
Why can NHibernate create a proxy for classes with properties that have private setters

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.