I have just started learning about templates , here I am facing problem in defining the derived class from the template class .
//template class in file1.h
template <typename KEY, typename VALUE>
class templclass{
map<KEY, VALUE> mymap;
public:
void storevalue(VALUE const& val)
{
mymap[KEY] = val;
}
}
//derived class in file2.h
class derivedclass: public class templclass<string,int>{
map<string,int> myintmap;
public:
void storeintval(int const& intval);
}
//methods oc derived class in file2.cpp
voidstoreintval(int const & intval)
{
//here I would like to class my template class method storevalue()
//but i tried all the ways reading many posts but my struggle went invain
}
could some one please help resolving this? Please let me know, if i am doing something wrong in defining the derived class.
//template class in file1.h
template <typename KEY, typename VALUE>
class templclass
{
std::map<KEY, VALUE> mymap;
public:
void storevalue( const KEY& key, const VALUE& value )
{ mymap[key] = value; }
};
//derived class in file2.h
class derivedclass: public templclass< std::string, int >
{
using base = templclass< std::string, int > ;
public:
void storeintval( const std::string& key, int value )
{ base::storevalue( key, value ) ; }
void storevalue( const std::string& key, int value )
{ base::storevalue( key, value ) ; }
};
okay got it , I am using the type as array index :(. I have corrected it as below
void storevalue(VALUE const& val)
{
KEY keyval;
//keyval will be obtained from some method call depending on type
mymap[keyval] = val;
}
Thanks . I have corrected it accordingly, but my problem is in defining the derived class and its respective methods . Could you please correct my derived class definition and method storeintval() definition
But i wanted to store my 'int' values in a derived class map attribute ( map<string,int> myintmap>
1 2 3 4 5 6 7 8 9 10
//derived class in file2.h
class derivedclass: public templclass< std::string, int >
{
using base = templclass< std::string, int > ;
// map<string,int> myintmap>; need to store my int values in myintmap
public:
//with the below approach i cannot store the values
void storeintval( const std::string& key, int value )
{ base::storevalue( key, value ) ; }
};
could you please also help me in resolving this ?
Thanks in advance!
sorry for the confusion. Actually , i couldn't able to properly frame the question at start . Initially I felt I need separate map for each derived class i newly define. But after seeing solution provided by Mr.JLBorges , I felt its not required to provide a new definition of map each time(each derived class)