mapped maps in classes

I am trying to write a map in a map, but wrapped in a class. I stripped down the code to bare bones:
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
#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <sstream>
#include <iomanip>
#include <initializer_list>

template <class KEY, class VALUE>
class Dict{
    public:
        std::map<KEY, VALUE> dictionary;
        
        void update(KEY k, VALUE v){
            dictionary[k] = v;
        }
};

int main(){
    Dict<std::string, std::string> d;
    
    Dict<std::string, Dict> m;
    //m['map'].display();
    m.update("map", d);
}



and the error i get is:
1
2
3
4
5
6
7
8
9
10
11
12
test2.cpp: In function 'int main()':
test2.cpp:23:27: error: type/value mismatch at argument 2 in template parameter list for 'template<class KEY, class VALUE> class Dict'
     Dict<std::string, Dict> m;
                           ^
test2.cpp:23:27: error:   expected a type, got 'Dict'
test2.cpp:23:30: error: invalid type in declaration before ';' token
     Dict<std::string, Dict> m;
                              ^
test2.cpp:26:7: error: request for member 'dictionary' in 'm', which is of non-class type 'int'
     m.dictionary["map"] = d;
       ^


I dont understand why it says it expects a type and got Dict?
Last edited on
1
2
3
Dict<std::string, Dict> m;
                // ^
                // Your problem 


Dict is not a class. It's a template. You can't say your value is a Dict... because what is it a Dict of? Strings?

You probably meant to do this:
Dict<std::string, Dict<std::string,std::string>> m;

Or... if you want to typedef for clarity:
1
2
3
typedef Dict<std::string,std::string> StringDict;

Dict<std::string, StringDict> m;
You probably meant to do this:
Dict<std::string, Dict<std::string,std::string>> m;

ug, im an idiot. Yes thats what i meant. Thank you.
Topic archived. No new replies allowed.