首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为了调用“`cout << std::vector<_Ty>`”,需要包含哪个标题?

为了调用“`cout << std::vector<_Ty>`”,需要包含哪个标题?
EN

Stack Overflow用户
提问于 2012-12-12 13:37:11
回答 1查看 1.4K关注 0票数 0

我试图遵循Boost程序选项库教程的选项Details部分,并得到以下错误:

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

我的密码在下面。我猜我需要包括一个标题,但我不知道是哪一个。

代码语言:javascript
复制
#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);
}
EN

回答 1

Stack Overflow用户

发布于 2012-12-12 14:09:59

严格地说,这不是对我问题的回答(我更喜欢这样做的标准库特性),但我发现了类似的问题,它提供了一个在接受<<对象的ostream类上实现vector操作符的类:

代码语言:javascript
复制
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++给出了错误

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13841036

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档