2012-08-10 68 views
10

我確實希望給出位置參數的默認值,如代碼中的註釋,但編譯器會抱怨。代碼,因爲它編譯得很好。我使用升壓1.46.1和g ++如何在boost :: program_options中指定向量<string>的默認值

int main(int argc, char *argv[]) { 
    namespace po = boost::program_options; 

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

    po::options_description desc("Options"); 
    std::vector<std::string> vec_str; 
    std::string str; 
    desc.add_options() 
     ("foo,f", po::value<std::string>()->default_value(str), "bar") 
     //("path,p", po::value< std::vector<std::string> >()->default_value(vec_str), "input files.") 
     ("path,p", po::value< std::vector<std::string> >(), "input files.") 
    ; 

    po::variables_map vm; 
    po::store(po::command_line_parser(argc, argv).options(desc).positional(p).run(), vm); 
    po::notify(vm); 
} 
+1

[這是否有幫助?](http://stackoverflow.com/a/3152802/220636) – nabulke 2012-08-10 12:08:50

+0

這一行彙編:
(「path,p」,po :: value > - > default_value(std :: vector (),「」),「input files。」)
但我不知道爲什麼 – Michael 2012-08-10 13:49:47

+0

@nabulke是的,它確實有幫助 – Michael 2012-08-10 13:52:32

回答

11

我需要給默認值的文本表示,看到http://lists.boost.org/boost-users/2010/01/55054.php

I.e.以下行的工作:

("path,p", po::value< std::vector<std::string> >()->default_value(std::vector<std::string>(), ""), "input files.") 

我想這是需要您的幫助輸出,可在我的例子

std::cout << desc << std::endl; 

由於編譯器不知道如何超載operator<<()vector<string>,它抱怨。