class Map {
private:
std::vector<std::string> key;
std::vector<std::string> storage;
int i;
public:
Map();
Map* set(std::string, std::string);
std::string get(std::string);
};
Map::Map() {}
Map* Map::set(std::string k, std::string v) {
key.push_back(k);
storage.push_back(v);
i++;
return (this);
}
std::string Map::get(std::string k) {
for (int k = 0; k < i; i++)
if (key[i] == k)
return storage[i];
}
I’m still playing around with C++ and classes this time. I haven’t “studied” maps and vectors yet, just read some documentation. This class serves no purpose but to try things out, so: yes, i know something similar to what I’m trying to achieve here already exists.
Why, compiling this code, am I getting:
main.cpp:32: error: no match for ‘operator==’ in
‘((Map*)this)->Map::key. std::vector<_Tp, _Alloc>::operator[] [with
_Tp = std::basic_string, std::allocator >, _Alloc =
std::allocator,
std::allocator > >](((long unsigned int)((Map*)this)->Map::i))
== k’
I mean, is that for real that the == operator doesn’t exists in vector?
The reason is shadowing.
Your int k shadows your parameter std::string k, so the compiler sees string == int and there is no such comparator.