1

I'm writing a program with use of boost::program_option but I can't use one of its feature:

 po::options_description desc("Allowed options");
desc.add_options()
    ("include-path,I", po::value< std::vector<std::string> >(), "include path");

po::variables_map vm;
po::store(po::parse_command_line(argc, argv, desc), vm);
po::notify(vm);

if (vm.count("include-path"))
{
    std::cout << "Include paths are: " 
         << vm["include-path"].as< std::vector<std::string> >() << "\n";
}

It's pretty the same as in boost.tutorial (http://www.boost.org/doc/libs/1_57_0/doc/html/program_options/tutorial.html)

I Get such error: error: cannot bind ‘std::basic_ostream’ lvalue to ‘std::basic_ostream&&’ std::cout << "Include paths are: " << vm["include-path"].as>() << std::endl;

I've read some topics such as: error: cannot bind ‘std::basic_ostream’ lvalue to ‘std::basic_ostream&&’ Overloading operator<<: cannot bind lvalue to ‘std::basic_ostream&&’

But I don't see connection with my problem. My platform: Fedora 20, Gcc 4.8.3, boost_1_57_0, I'm using -std=c++11 flat to compile code.

Community
  • 1
  • 1
kotu
  • 90
  • 1
  • 8

1 Answers1

2

You can't print a vector<std::string> like that. This has nothing to do with Boost or Program Options:

std::vector<std::string> v = vm["include-path"].as< std::vector<std::string> >();
std::cout << "Include paths are: ";
for (auto& p : v)
    std::cout << "\n\t" << p;

Live On Coliru

#include <boost/program_options.hpp>
#include <iostream>

namespace po = boost::program_options;

int main(int argc, char** argv) {
    po::options_description desc("Allowed options");
    desc.add_options()
        ("include-path,I", po::value< std::vector<std::string> >(), "include path");

    po::variables_map vm;
    po::store(po::parse_command_line(argc, argv, desc), vm);
    po::notify(vm);

    if (vm.count("include-path"))
    {
        std::vector<std::string> v = vm["include-path"].as< std::vector<std::string> >();
        std::cout << "Include paths are: ";
        for (auto& p : v)
            std::cout << "\n\t" << p;
    }
}
sehe
  • 374,641
  • 47
  • 450
  • 633
  • Thank you, it works. So there is a mistake in tutorial in boost/documentation. – kotu Jan 15 '15 at 09:10
  • 1
    @kotu no, actually not; in the actually `example/multiple_source.cpp` there is this helper http://paste.ubuntu.com/9754725/ to make it JustWork™ :) – sehe Jan 15 '15 at 09:31
  • ok. Now everything is clear. I've just started my adventure with boost ;] – kotu Jan 15 '15 at 13:23