c++ - boost::PO 不能绑定(bind)到 ‘std::basic_ostream<char>&&’

标签 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’左值绑定(bind)到‘std::basic_ostream&&’ std::cout << "包含路径是:"<< vm["include-path"].as>() << 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 来编译代码。

最佳答案

你不能打印 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;
    }
}

关于c++ - boost::PO 不能绑定(bind)到 ‘std::basic_ostream<char>&&’,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27950777/

相关文章:

c++ - fork 并执行许多不同的进程,并从每个进程中获取结果

c++ - 在 openGL 中使用着色器显示体素

c++ - 为什么我的无锁消息队列段错误 :(?

c++ - 不明确的元函数或未定义的类型

c++ - eclipse c++ 没有构建错误

c++ - Arcball 相机在 90 度方位角倒转

c++ - 为什么没有 std::shared_ptr<T[]> 特化?

c++ - 在 C++11 中使用 sort 函数有什么问题?

c++ - 如何根据类型是整型还是浮点型来更改模板方法?

c++ - 如何将 boost::Makevariantover 转换为 vector