Reputation: 2265
For some reason, I am having trouble editing values in my unordered_map, and am wondering what I'm doing wrong.
In the following code, parameter
is a struct. For some reason, the following code is throwing a syntax error, not liking the [.
void MyClass::setParameter(string name, parameter param) {
if (this->param_name_to_data == nullptr) {
//create it lazily
this->param_name_to_data = new unordered_map<string, parameter>();
}
this->param_name_to_data->[name] = param;
}
The dictionary id declared in the corresponding .h file as:
private:
std::unordered_map<std::string, parameter> * param_name_to_data = nullptr;
What am I doing wrong?
Upvotes: 0
Views: 1454
Reputation: 26536
param_name_to_data->[name] = param;
is not a valid syntax
when the compiler sees ->
it looks for either a member variable or a member function. the statement ->[...]
is meaningless since []
alone is neither a member variable nor a function.
you can write instead
(*param_name_to_data)[name] = param;
or turn the pointer to reference first
auto& map = *param_name_to_data;
map[name] = param;
you can also use the ugly literal form of
param_name_to_data->operator[] (name) = param
but the last one is discouraged.
Upvotes: 4