【问题标题】:Vector string with boost library C++ gives error带有 boost 库 C++ 的向量字符串给出错误
【发布时间】:2012-03-13 20:48:17
【问题描述】:

这是我想写的代码。

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

编译器对我要打印 include-path 参数的最终 if 语句给出错误。它给出的错误是

rx_timed_samples.cpp:62:96: 错误:'std::operator(const std::allocator)(& std::allocator()))))))->boost::program_options::variable_value:: 与 T = std ::vector, std::allocator > >'

我不明白?请帮忙。

【问题讨论】:

    标签: c++ boost vector


    【解决方案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;
    }
    

    【讨论】:

      【解决方案2】:

      我认为这里的问题是没有为std::vector&lt;std::string&gt; 定义

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-02-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-09-30
        • 2021-06-06
        • 2023-03-29
        • 1970-01-01
        相关资源
        最近更新 更多