我有以下简单的代码:
#include <boost/program_options.hpp>
#include <iostream>
#include <iterator>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
namespace po = boost::program_options;
int main(int argc, char *argv[])
{
po::options_description config("Configuration");
config.add_options()
("optimization", po::value<int>()->default_value(10),
"optimization level")
("include-path,I",
po::value< vector<string> >()->composing(),
"include path")
;
po::variables_map vm;
po::store(po::parse_command_line(argc, argv, config), vm);
auto popts = po::parse_config_file<char>("config.ini",config,true);
po::store(popts,vm);
po::notify(vm);
for(auto o:popts.options) {
cout << o.string_key << "=" << "\n";
std::copy(o.value.begin(), o.value.end(), std::ostream_iterator<char>(std::cout, " "));
}
return 0;
}
我尝试使用gcc编译:
g++ -std=c++11 test.cpp
这给了我以下错误:
In file included from /usr/include/c++/6.2.1/bits/char_traits.h:39:0,
from /usr/include/c++/6.2.1/string:40,
from /usr/include/boost/program_options/errors.hpp:12,
from /usr/include/boost/program_options/options_description.hpp:12,
from /usr/include/boost/program_options.hpp:15,
from test.cpp:1:
/usr/include/c++/6.2.1/bits/stl_algobase.h: In instantiation of ‘static _OI std::__copy_move<false, false, std::random_access_iterator_tag>::__copy_m(_II, _II, _OI) [with _II = std::__cxx11::basic_string<char>*; _OI = std::ostream_iterator<char>]’:
/usr/include/c++/6.2.1/bits/stl_algobase.h:386:44: required from ‘_OI std::__copy_move_a(_II, _II, _OI) [with bool _IsMove = false; _II = std::__cxx11::basic_string<char>*; _OI = std::ostream_iterator<char>]’
/usr/include/c++/6.2.1/bits/stl_algobase.h:422:45: required from ‘_OI std::__copy_move_a2(_II, _II, _OI) [with bool _IsMove = false; _II = __gnu_cxx::__normal_iterator<std::__cxx11::basic_string<char>*, std::vector<std::__cxx11::basic_string<char> > >; _OI = std::ostream_iterator<char>]’
/usr/include/c++/6.2.1/bits/stl_algobase.h:455:8: required from ‘_OI std::copy(_II, _II, _OI) [with _II = __gnu_cxx::__normal_iterator<std::__cxx11::basic_string<char>*, std::vector<std::__cxx11::basic_string<char> > >; _OI = std::ostream_iterator<char>]’
test.cpp:33:96: required from here
/usr/include/c++/6.2.1/bits/stl_algobase.h:324:18: error: no match for ‘operator=’ (operand types are ‘std::ostream_iterator<char>’ and ‘std::__cxx11::basic_string<char>’)
*__result = *__first;
~~~~~~~~~~^~~~~~~~~~
In file included from /usr/include/c++/6.2.1/iterator:66:0,
from /usr/include/boost/detail/iterator.hpp:11,
from /usr/include/boost/iterator/iterator_traits.hpp:8,
from /usr/include/boost/range/iterator_range_core.hpp:26,
from /usr/include/boost/lexical_cast.hpp:30,
from /usr/include/boost/program_options/value_semantic.hpp:14,
from /usr/include/boost/program_options/options_description.hpp:13,
from /usr/include/boost/program_options.hpp:15,
from test.cpp:1:
/usr/include/c++/6.2.1/bits/stream_iterator.h:193:7: note: candidate: std::ostream_iterator<_Tp, _CharT, _Traits>& std::ostream_iterator<_Tp, _CharT, _Traits>::operator=(const _Tp&) [with _Tp = char; _CharT = char; _Traits = std::char_traits<char>]
operator=(const _Tp& __value)
^~~~~~~~
/usr/include/c++/6.2.1/bits/stream_iterator.h:193:7: note: no known conversion for argument 1 from ‘std::__cxx11::basic_string<char>’ to ‘const char&’
/usr/include/c++/6.2.1/bits/stream_iterator.h:154:11: note: candidate: std::ostream_iterator<char>& std::ostream_iterator<char>::operator=(const std::ostream_iterator<char>&)
class ostream_iterator
^~~~~~~~~~~~~~~~
/usr/include/c++/6.2.1/bits/stream_iterator.h:154:11: note: no known conversion for argument 1 from ‘std::__cxx11::basic_string<char>’ to ‘const std::ostream_iterator<char>&’
我错过了什么?这是某种ABI不兼容吗?
我正在使用gcc 6.2.1并提升1.61。
(注意:是的我也需要链接boost库,但问题是编译器错误。)
答案 0 :(得分:1)
自己发现错误。
而不是
std::copy(o.value.begin(), o.value.end(), std::ostream_iterator<char>(std::cout, " "));
应该是
std::copy(o.value.begin(), o.value.end(), std::ostream_iterator<string>(std::cout, " "));
(错误的模板参数char
而不是string
)。