简体   繁体   中英

no match for 'operator=' when searching for value in map

I'm a c++ noob and I can't figure this one out. I'm trying to search for a value in a map using the module parameter as a key as seen below. The first error I get is a no match for operator equals on the line indicated below and the second error is a "expected primary-expression before ')' token" on the line shown below.

float Student::getMark(const string &module) const throw (NoMarkException){ //TODO
    map<string, float>::iterator p;
    p = marks.find(module); //no match for operator=
    if(p != marks.end())
        return p->second;
    else
        throw (NoMarkException); //expected primary-expression before ')' token
    return 0.0; 
}

Any help would be much appreciated!

Your member function is marked const . That means all members you access in the function are also const qualified. This means when you call find it returns a const_iterator instead of an iterator . You can fix it by using

map<string, float>::const_iterator p;

Or even easier with

auto p = marks.find(module);

As far as your error with throwing the exception you should be creating a object to throw. That means you need throw throw NoMarkException(); not throw (NoMarkException);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM