boost :: PO并且无法绑定到'std :: basic_ostream <char>&amp;&amp;'</char>

时间:2015-01-14 19:20:25

标签: c++ c++11 boost

我正在编写一个使用boost :: program_option的程序,但我不能使用它的一个功能:

 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";
}

它与boost.tutorial(http://www.boost.org/doc/libs/1_57_0/doc/html/program_options/tutorial.html

中的相同

我收到这样的错误: 错误:无法将'std :: basic_ostream'左值绑定到'std :: basic_ostream&amp;&amp;' std :: cout&lt;&lt; “包括路径是:”&lt;&lt; vm [“include-path”]。as&gt;()&lt;&lt;的std :: ENDL;

我读过一些话题,例如: error: cannot bind ‘std::basic_ostream’ lvalue to ‘std::basic_ostream&&’ Overloading operator<<: cannot bind lvalue to ‘std::basic_ostream&&’

但我没有看到与我的问题有关。 我的平台:Fedora 20,Gcc 4.8.3,boost_1_57_0,我正在使用-std = c ++ 11 flat来编译代码。

1 个答案:

答案 0 :(得分:2)

您无法像这样打印vector<std::string>。这与Boost或程序选项无关:

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;
    }
}