我正在使用 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;
#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/