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

  • Home
  • SEARCH
  • 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 3855896
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T17:50:48+00:00 2026-05-19T17:50:48+00:00

I am attempting to store objects derived from a templated base class in an

  • 0

I am attempting to store objects derived from a templated base class in an STL map. However, attempting to insert derived (or indeed base) objects returns:

C2440 'initializing' : cannot convert from 'CBase<T> ' to 'CBase<T>'

I understand using derived classes is one accepted way of making an STL container heterogenous (http://www.parashift.com/c++-faq-lite/containers.html#faq-34.4). I would like to know if it is possible to use a template in this context. It would be very convenient as I could have a single declaration of a range of containers in the base class that are instantiated for my various types at compile time, instead of duplicate declarations in non-templated derived classes.

My code is as follows:

//Header
using namespace std;

template<class T>
class CBase
{
    public:
        CBase::CBase() {};
        virtual CBase::~CBase() {};
        vector<pair<int, T> > RetrieveVect() { return vect; };

    private:
        vector<pair<int, T> > vect;
};

class CDerivedString : public CBase<string>
{
    ...
};

class CDerivedInt : public CBase<int>
{
    ...
};

//cpp
int main(void)
{
    //Map specialised for pointer to base class
    map<string, CBase<class T>* > m_myMap;

    string s = "key";

    //Create and insert object (base class)
    CBase<int> *dataInt = new CBase();
    //The following results in error C2440: 'initializing' : cannot convert from 'CBase<T> ' to 'CBase<T>
    m_myMap.insert(std::make_pair(s, dataInt));

    //Create and insert object (derived class)
    CBase<int> *dataBase = new CBase<int>();
    //The following results in error C2440: 'initializing' : cannot convert from 'CBase<T> ' to 'CBase<T>
    m_myMap.insert(pair<string, CBase<class T>* >(s, static_cast<CBase*>(dataInt)));
}

I’ve tried doing a dynamic_cast on the derived class pointer to cast it to a base pointer type but this didn’t work either:

//error C2440: 'static_cast' : cannot convert from 'CBase<T> *' to 'CBase<T> *'
m_myMap.insert(pair<string, CBase<class T>* >(s, static_cast<CBase<class T>*>(dataInt)));  
  • 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-19T17:50:48+00:00Added an answer on May 19, 2026 at 5:50 pm

    The following line:

    map<string, CBase<class T>* > m_myMap;
    

    almost certainly does not mean what you think it does. This is equivalent to:

    map<string, CBase<T>* > m_myMap;
    

    That is: ‘T’ is a concrete class, not a template parameter. There is of course no relationship between the classes:

    CBase<int> 
    

    and

    CBase<T>
    

    Hence the error message – you have never defined (or intended to) the concrete class ‘T’. Take SCFrench’s comment re using the correct base and then use that in the map<>:

    map<string, CBase<int>* > m_myIntMap;
    

    will allow you to store the concrete CDerivedInt* objects. If you want to store any object, define a fully generic base:

     class CBaseAbc 
         { 
            virtual ~CBaseAbc() = 0;
         };
     template<class T>
     class CBase : public CBaseAbc 
        {
         // etc.
        };
    
    map<string, CBaseAbc* > m_myAnthingMap;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im attempting to store json objects in a cookie, but im running into a
I am trying to store some objects in an SQL Server Compact (SQL CE)
I'm attempting to store 3 values in a database I've created on an android
I'm attempting to make a simple game of Pairs for Android. Program Structure Menu.java
I'm attempting to run some unit tests on my application using SQLite in memory,
I have a vote model, which has a class method called score . Basically,
I have a date field stored in the format 16/08/2010 17:17 I am attempting
I just don't understand what's going on here and I've spent a lot of
I recently was working with some dynamic Search Expressions and ran into a bit
I'm writing a script which pulls out some patient data and generates an XML

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.