How to read C ++ errors using templates

I am learning C ++. I regularly get errors like this

/usr/lib/gcc/i686-pc-cygwin/4.3.4/include/++/bits/basic_string.h: 1458: instance from 'static _CharT * std :: basic_string <_CharT, _Traits, _Alloc> :: _S_construct_aux (_InIterator, _InIterator, const _Alloc &, std :: __ false_type) [with _InIterator = std :: istream_iterator, std :: allocator>, char, std :: char_traits, int>, _CharT = char, _Traits = std :: char_traits, _Alloc = std :: allocator] '

How can I understand this in order to have at least some place to find a solution?

If you are interested, the source code:

#include <iostream> #include <fstream> #include <string> #include <iterator> using namespace std; int main(int argc, char **argv) { string fileName = "example.txt"; ifstream ifile(fileName.c_str()); istream_iterator<string> begin(ifile); istream_iterator<string> end; string s(begin,end); cout << s; } 
+7
c ++
source share
5 answers

This is not the whole mistake, just a description of one instance.

Basically you care about:

1) which line in your code caused the error ( string s(begin,end); )

2) what error led to (for example, cannot convert 'const std::basic_string<char, std::char_traits<char>, std::allocator<char> >' to 'char' in assignment ) and where

3) you can compare what the compiler says about the template parameters and what you assume.

Probably, some experience is required in order not to be afraid of mistakes, and this, of course, helps to know the library well.

In this case, the reason is that the string constructor expects a range of characters, but you are passing a series of strings.

+6
source share

Use STLFilt .

+5
source share

Como has a much friendlier compiler, check it online: http://www.comeaucomputing.com/tryitout/

for example, this is an error:

 Comeau C/C++ 4.3.10.1 (Oct 6 2008 11:28:09) for ONLINE_EVALUATION_BETA2 Copyright 1988-2008 Comeau Computing. All rights reserved. MODE:strict errors C++ C++0x_extensions "string", line 1175: error: no suitable conversion function from "const std::string" to "char" exists push_back(*__first); ^ detected during instantiation of "std::basic_string<_CharT, _Traits, _Alloc> &std::basic_string<_CharT, _Traits, _Alloc>::append(_InputIter, _InputIter, std::input_iterator_tag) [with _CharT=char, _Traits=std::char_traits<char>, _Alloc=std::allocator<char>, _InputIter=std::istream_iterator<std::string, char, std::char_traits<char>, ptrdiff_t>]" at line 624 1 error detected in the compilation of "ComeauTest.c". 

which leads me to assume that the constructor expects a char iterator, not a line iterator (as in many lines):

 istream_iterator<char> begin(ifile); istream_iterator<char> end; 
+3
source share

In fact, you did not specify the key parts of the error message. You get two repeated errors in g ++ 4.2. The key parts are: /path/basic_string.tcc:103: error: cannot convert 'const std::basic_string<char, std::char_traits<char>, std::allocator<char> >' to 'char' in assignment and str_iter_test.C:15: instantiated from here .

The first part means that it cannot convert from a string to char, and the second part indicates which line the error occurred on, in this case string s(begin,end); .

In this case, your iterators should go through char not string :

 istream_iterator<char> begin(ifile); istream_iterator<char> end; 
+2
source share

The specific error message you are showing should be part of the error released by g ++. The full error should look something closer to the following (I shortened the paths, and this conclusion 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. 

It's not very friendly, 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 the reverse order: the first error is the actual error, then the following lines give you crackers through a source showing how the compiler got there. The last line shows where your source had an error:

 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 t.cpp:16 example file, there is the following line:

 string s(begin,end); 

If you comment out this line (and the next line using s ), you will find that your source compiles without errors.

At this point, it should be clear enough that you are using the std::string constructor incorrectly, which means that you are passing the wrong argument types to it. You give it something that has std::string (which is what std::basic_string<char> ) and expects something that has char .

If you consult the documentation for std::string , you will find that it has a constructor that looks like this:

 template <typename InputIterator> string(InputIterator first, InputIterator last); 

These input iterators allow you to create a string from a range of characters. You, however, pass the constructor the std::string construct. If you change your istream_iterator<string> to istream_iterator<char> , that should fix the error.

+1
source share

All Articles