I am implementing a class that involves unordered_sets, and unordered_maps. When I go to run the program it gives me the following error
error: no match for ‘operator[]’ (operand types are ‘std::unordered_set<std::__cxx11::basic_string<char> >’ and ‘const string’ {aka ‘const std::__cxx11::basic_string<char>’})
int points = Prizes[Codes[code]]->points;
I am unsure of how to fix this, please help.
I have pasted my function (sightly condensed) below.
int Code_Processor::Enter_Code(const string &username, const string &code) {
User *user = user_iter->second;
int points = Prizes[Codes[code]]->points;
user->points += points;
Codes.erase(code);
return points;
}
I have also included part of the header file below.
class User {
public:
std::string username;
std::string realname;
int points;
std::set <std::string> phone_numbers;
};
class Prize {
public:
std::string id;
std::string description;
int points;
int quantity;
};
class Code_Processor {
public:
int Enter_Code(const std::string &username, const std::string &code);
protected:
std::unordered_map <std::string, User *> Names;
std::unordered_map <std::string, User *> Phones;
std::unordered_set <std::string> Codes;
std::unordered_map <std::string, Prize *> Prizes;
};
The class template std::unordered_set
does not have the subscript operator.
You could write for example
if ( Codes.find( code ) != Codes.end() )
{
int points = Prizes[code]->points;
user->points += points;
Codes.erase(code);
}