2012-02-23 2 views
0

여기에 쓸 코드가 있습니다. 내가 포함 경로를 매개 변수를 인쇄 할 경우 if 문벡터 라이브러리의 부스트 라이브러리 C++에서 오류가 발생했습니다.

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

컴파일러는 마지막에 대한 오류를 제공합니다. 이주는 오차는

rx_timed_samples.cpp : 62 : 96 : 오류 : STD '에서'연산자 < < '에 대한 검색 연산자 :: < < ((* STD & [_Traits = 표준 : char_traits 함께] :: cout), ((const char *) "포함 경로 :") < < (& vm.boost :: program_options :: variables_map :: operator [] ((& std :: basic_string ((const char (boost :: program_options :: variable_value :: = "include-path"), ((const std :: allocator) & std :: allocator())))))))))))))))))))) : vector, std :: allocator >> '

알아 듣겠습니까? 도와주세요.

답변

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

여기서 문제는 < < 연산자가 std::vector<std::string>으로 정의되어 있다고 생각합니다. 이 호출에 필요한 것 :

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