Problem with map arguments in template class and definition of derived class

Hello,

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.

Thanks in advance!
Your storevalue() has an issue.

What is wrong in this:
1
2
3
std::map<int,double> foo;

foo[int] = 3.14;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//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
Hello JLBorges,

Thanks a lot for code:)

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!
Last edited on
I think its not required to create a separate map in the derived class. By defining a method to get the map in template class

1
2
3
4
5
6
7
  //definition of method to access the map elements
template <typename KEY, typename VALUE>
map<KEY, VALUE> templclass<KEY, VALUE>::gettemplmap() const
{
  
	return mymap;
}


by calling the above map in my derived class , I can access stored int values.
OP:
//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
Later:
But i wanted to store my 'int' values in a derived class map attribute
Still later:
I think its not required to create a separate map in the derived class.

Your changed your requirements mid-way, you were provided the correct solution as per your OP
Hello gunnerfunner,

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)
One option to consider is declaring the map in the base class with a protected access specifier.
http://en.cppreference.com/w/cpp/language/access

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <map>

template < typename KEY, typename VALUE >
class dict
{
    protected: std::map< KEY, VALUE > map;

    public: void store( const KEY& key, const VALUE& value ) { map[key] = value; }
};

#include <string>
#include <iostream>
#include <iomanip>

class dict_str_int: public dict< std::string, int >
{
    using base = dict< std::string, int > ;

    public:

        void store( const std::string& key, int value )
        { base::store( key, value ) ; }

        std::ostream& print( std::ostream& stm ) const
        {
            for( const auto& pair : map )
                stm << "{ " << std::quoted(pair.first) << " : " << pair.second << " } " ;
            return stm ;
        }
};

std::ostream& operator<< ( std::ostream& stm, const dict_str_int& d )
{ return d.print(stm) ; }

int main()
{
    dict_str_int d ;
    d.store( "twenty three", 23 ) ;
    d.store( "seventy", 70 ) ;
    std::cout << d << '\n' ;
}
Topic archived. No new replies allowed.