سؤال

I wrote this quick function to get familiar with boost::program_options. Please note that po is a 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();
    // also tried:  std::cout << *vm["infile"]

    return SUCCESS;
}  //application

When I comment out the second line in the function body, the application successfully compiles and prints the address of vm. However, when I try to compile with the function appearing as it does here, I get the following compiler insult:

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

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

What have I done wrong? Am I abusing a boost construct or am I getting mixed up in (de)referencing vm?

Update

Following the suggestion that I pass by reference to avoid the operator precedence issue, I rewrote my function thus:

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

    return SUCCESS;
}  //application

I'm now getting a different error:

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

What have I done wrong here?

Edit: I'd appreciate being told why my question is being downvoted. Is it too basic?

هل كانت مفيدة؟

المحلول

The [] operator has a higher precedence than the unary * operator. Thus, *vm["infile"] is the same as *(vm["infile"]), but you want (*vm)["infile"].

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top