这是我想写的代码。
int opt;
po::options_description desc("Allowed options");
desc.add_options()
("help","produce help message")
("compression",po::value<int>(&opt)->default_value(10),"optimization level")
("include-path,I", po::value< std::vector<std::string> >(), "include path")
("input file", po::value< std::vector<std::string> >(),"input file")
;
po::variables_map vm;
po::store(po::parse_command_line(argc,argv,desc),vm);
po::notify(vm);
if (vm.count("help")){
std::cout <<desc<<"\n";
return 1;
}
if (vm.count("compression")){
std::cout<<"Compression level was set to"<<vm["compression"].as<int>()<<".\n";
} else {
std::cout << "compression level was not set.\n";
}
if (vm.count("include-path")){
std::cout << "Include paths are: " << vm["include-path"].as< std::vector<std::string> > ()<< "\n";
}
编译器为最终的if语句提供错误,我想打印 include-path 参数。它给出的错误是
rx_timed_samples.cpp:62:96:错误:'std :: operator&lt;&lt;'中的'operator&lt;&lt;'不匹配[with _Traits = std :: char_traits]((*&amp; std :: cout),((const char *)“Include paths are:”))&lt;&lt; (&amp; vm.boost :: program_options :: variables_map :: operator []((*&amp; std :: basic_string(((const char *)“include-path”),((const std :: allocator) )(&amp; std :: allocator())))))) - &gt; boost :: program_options :: variable_value ::与T = std :: vector,std :: allocator&gt; &GT;” 的
我不明白吗?请帮忙。
答案 0 :(得分:1)
我认为这里的问题是没有&lt;&lt;运算符是为std::vector<std::string>
定义的。这次电话需要哪个:
std::cout << "Include paths are: " << vm["include-path"].as< std::vector<std::string> > ()<< "\n";
答案 1 :(得分:1)
您需要可以处理向量的流运算符的专门化。
template<class T>
std::ostream& operator <<(std::ostream& os, const std::vector<T>& v)
{
std::copy(v.begin(), v.end(), std::ostream_iterator<T>(std::cout, " "));
return os;
}