0

我正在尝试遵循Boost Program Options 库教程的选项详细信息部分,但出现以下错误:

error C2679: "binary '<<' : no operator found which takes a right-hand operand
of type 'const std::vector<_Ty>'" (or there is no acceptable conversion)

我的代码如下。我猜我需要包含一个标题,但我不知道是哪个。

#include <boost/program_options.hpp>
#include <iostream>
#include <vector>
#include <string>

using std::cout;
using std::endl;
using std::vector;
using std::string;

namespace po = boost::program_options;

int options_description(int ac, char* av[])
{
    int opt;
    po::options_description desc("Allowed options");
    desc.add_options()
        ("help", "produce help message")
        ("optimization", po::value<int>(&opt)->default_value(10), 
            "optimization level")
        ("include-path,I", po::value< vector<string> >(), "include path")
        ("input-file", po::value< vector<string> >(), "input file")
    ;

    po::positional_options_description p;
    p.add("input-file", -1);

    po::variables_map vm;
    po::store(po::command_line_parser(ac, av).
        options(desc).positional(p).run(), vm);
    po::notify(vm);

    if (vm.count("include-path"))
    {
        cout << "Include paths are: " 
             << vm["include-path"].as< vector<string> >() << "\n"; // Error
    }

    if (vm.count("input-file"))
    {
        cout << "Input files are: " 
             << vm["input-file"].as< vector<string> >() << "\n"; // Error
    }

    cout << "Optimization level is " << opt << "\n";   

    return 0;
}

int main(int argc, char *argv[])
{
    return options_description(argc, argv);
}
4

1 回答 1

0

严格来说不是我的问题的答案(我更喜欢这样做的标准库功能),但我发现类似的问题有一个答案,该答案提供了一个在接受对象的类上实现<<运算符的类:ostreamvector

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

我将它添加到我的代码中,它现在可以编译。太糟糕了,这在教程中没有提到。

来源:带有 boost 库 C++ 的向量字符串给出错误

于 2012-12-12T14:09:59.630 回答