簡體   English   中英

boost :: PO並且不能綁定到&#39;std :: basic_ostream <char> &amp;&amp;”

[英]boost::PO and cannot bind to ‘std::basic_ostream<char>&&’

我正在使用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'左值綁定到'std :: basic_ostream &&'std :: cout <<“包含路徑為:” << vm [“ include-path”]。as>() << std :: endl;

我讀過一些主題,例如: 錯誤:無法將'std :: basic_ostream'左值綁定到'std :: basic_ostream &&' 重載運算符<<:無法將左值綁定到'std :: basic_ostream &&'

但是我看不到與我的問題有關的聯系。 我的平台:Fedora 20,Gcc 4.8.3,boost_1_57_0,我正在使用-std = c ++ 11 flat來編譯代碼。

您不能像這樣打印vector<std::string> 這與Boost或Program Options無關:

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;

生活在Coliru

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM