c++ - Why is this use of the [] operator causing a compiler error? -


i wrote quick function familiar boost::program_options. please note po namespace alias, defined thus: namespace po = boost::program_options.

int application(po::variables_map* vm) {     std::cout << vm << std::endl;     std::cout << *vm["infile"].value();     // tried:  std::cout << *vm["infile"]      return success; }  //application 

when comment out second line in function body, application compiles , prints address of vm. however, when try compile function appearing here, following compiler insult:

invalid types ‘boost::program_options::variables_map*[const char [7]]’ array subscript 

i should note replacing second line std::cout << vm->count("infile") returns 1.

what have done wrong? abusing boost construct or getting mixed in (de)referencing vm?

update

following suggestion pass reference avoid operator precedence issue, rewrote function thus:

int application(po::variables_map& vm) {     std::cout << &vm << std::endl;     std::cout << vm["infile"].value();      return success; }  //application 

i'm getting different error:

no match ‘operator<<’ (operand types ‘std::ostream {aka std::basic_ostream<char>}’ , ‘const boost::program_options::variable_value’) 

what have done wrong here?

edit: i'd appreciate being told why question being downvoted. basic?

the [] operator has higher precedence unary * operator. thus, *vm["infile"] same *(vm["infile"]), want (*vm)["infile"].


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -