0
votes

here is a code which i want to write.

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

The compiler gives error for the final if statement where i want to print the include-path parameter. the error it gives is

rx_timed_samples.cpp:62:96: error: no match for ‘operator<<’ in ‘std::operator<< [with _Traits = std::char_traits]((* & std::cout), ((const char*)"Include paths are: ")) << (& vm.boost::program_options::variables_map::operator[]((* & std::basic_string(((const char*)"include-path"), ((const std::allocator)(& std::allocator()))))))->boost::program_options::variable_value::as with T = std::vector, std::allocator > >’

i dont get it? please help.

2

2 Answers

1
votes

You need a specialization of the stream operator that can handle vectors.

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
votes

I think the problem here is that no << operator is defined for std::vector<std::string>. Which is needed for this call:

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