The particular error message you show should only be part of the error issued by g++. The full error should look something closer to the following (I've shortened the paths, and this output is from 4.1.2):
basic_string.tcc: In static member function 'static _CharT* std::basic_string<_CharT, _Traits, _Alloc>::_S_construct(_InIterator, _InIterator, const _Alloc&, std::input_iterator_tag) [with _InIterator = std::istream_iterator<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, char, std::char_traits<char>, int>, _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]':
basic_string.h:1449: instantiated from 'static _CharT* std::basic_string<_CharT, _Traits, _Alloc>::_S_construct_aux(_InIterator, _InIterator, const _Alloc&, __false_type) [with _InIterator = std::istream_iterator<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, char, std::char_traits<char>, int>, _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]'
basic_string.h:1464: instantiated from 'static _CharT* std::basic_string<_CharT, _Traits, _Alloc>::_S_construct(_InIterator, _InIterator, const _Alloc&) [with _InIterator = std::istream_iterator<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, char, std::char_traits<char>, int>, _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]'
basic_string.tcc:241: instantiated from 'std::basic_string<_CharT, _Traits, _Alloc>::basic_string(_InputIterator, _InputIterator, const _Alloc&) [with _InputIterator = std::istream_iterator<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, char, std::char_traits<char>, int>, _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]'
t.cpp:16: instantiated from here
Line 101: error: cannot convert 'const std::basic_string<char, std::char_traits<char>, std::allocator<char> >' to 'char' in assignment
compilation terminated due to -Wfatal-errors.
That's not much friendlier, is it? :-) The useful lines in the error are the lines at the end, not the lines at the beginning. The error message is issued in reverse order: the first error is the actual error, then the subsequent lines give you breadcrumbs through the source showing how the compiler got there. The last line shows where in your source the error was:
t.cpp:16: instantiated from here
Line 101: error: cannot convert '
const std::basic_string<char, std::char_traits<char>, std::allocator<char> >' to
'char' in assignment
In my example file, t.cpp:16
is the following line:
string s(begin,end);
If you comment out that line (and the line following it that uses s
), you'll find that your source compiles without error.
At this point, it should be fairly clear that you are using the std::string
constructor incorrectly, which means you are passing it the wrong types of arguments. You are giving it something that has a std::string
(that's what std::basic_string<char>
is) and it is expecting something that has a char
.
If you consult the documentation for std::string
, you'll find that it does have a constructor that looks like:
template <typename InputIterator>
string(InputIterator first, InputIterator last);
Those input iterators allow you to create a string from a range of characters. You, however, are passing a range of std::string
s to the constructor. If you change your istream_iterator<string>
s to istream_iterator<char>
that should fix the error.