简体   繁体   中英

C++ Compilation Error Non Class Type

In below I am getting error as error: request for member 'get_id' in '* it', which is of non-class type 'const Param* const'. What is the problem with below piece of code

bool SomeParams::is_default(int _id) const
{
        vector<const Param*> param_list;
        bool is_default = false;

        if( get_default_params(param_list) ) // This populates param_list
        {
                vector<const Param*>::const_iterator it = param_list.begin();

                for(;it!=param_list.end();++it)
                {
                        if( *it->get_id() == _id ) // get_id is function in Param object
                        {
                                is_default = true;
                                break;
                        }
                }
        }

        return is_default;
}

Precendece of -> (ie member access operator) is higher than * (ie indirection operator), so

*it->get_id();

is interpreted as:

*(it->get_id()); 

which causes compilation error.

What you need is this:

(*it)->get_id();

See this table:


Two important points:

  • The name of the parameter of the function starts with _ , which according to the language specification, invokes undefined behavior. Names starting with an underscore are reserved. Don't use them.

  • The function's name is is_default , and there is one variable inside the function, which is also is_default . Why don't you choose different name for the variable? That would increase the readability of your code.

*it括在括号中: (*it)->get_id()

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