如何读取包含模板的c++错误

时间:2022-10-28 10:48:10

I'm learning C++. I regularly get errors that look like this

我正在学习c++。我经常会遇到这样的错误

/usr/lib/gcc/i686-pc-cygwin/4.3.4/include/c++/bits/basic_string.h:1458: instantiated 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]'

/usr/lib/gcc/i686-pc-cygwin / 4.3.4包括/ c++ /位/ basic_string。h:1458:从“静态_CharT* std::basic_string<_CharT, _Traits, _Alloc>:::_S_construct_aux(_InIterator, _InIterator, const _Alloc&, std:: false_type)中实例化

How do I make sense of this so that I can at least have some place to search for a solution?

我如何理解这个,以便我至少有地方去寻找一个解决方案?

In case you're interested, the original code is:

如果您感兴趣,原始代码是:

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

5 个解决方案

#1


6  

This is not the whole error, just a description of one instantiation.

这不是整个错误,只是一个实例化的描述。

Basically you care about:

基本上你关心:

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

1)代码中的哪一行触发了错误(字符串s(开始,结束);

2) what error did it result in (e.g cannot convert 'const std::basic_string<char, std::char_traits<char>, std::allocator<char> >' to 'char' in assignment) and where

2)什么错误导致了(e)。g不能转换'const std::basic_string , std::allocator >' to 'char' in assignment)和where。 ,>

3) you might compare what the compiler is saying the template parameters are, and what you assume them to be.

3)您可以比较编译器所说的模板参数是什么,以及您认为它们是什么。

It probably takes some experience not to be scared of the errors, and it certainly helps to know the library well.

要避免错误,可能需要一定的经验,了解这个库当然很有帮助。

In this case the cause is that the string's constructor is expecting a range of characters, but you are passing a range of strings.

在这种情况下,原因是字符串的构造函数期望字符的范围,但是您正在传递字符串的范围。

#2


5  

Use STLFilt.

使用STLFilt。

#3


3  

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

他有很多更友好的编译器,可以在线查看:http://www.comeaucomputing.com/tryitout/。

for example, this is the 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 constructor expects char iterarator rather than iterator over strings (as in many strings):

这使我假定构造函数期望char迭代器而不是字符串(像许多字符串一样):

istream_iterator<char> begin(ifile);
istream_iterator<char> end;

#4


2  

You actually omitted the key parts of the error message. You get two duplicate 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.

实际上,您省略了错误消息的关键部分。在g++ 4.2中有两个重复的错误。关键部分是:/path/basic_string。错误:不能将“const std:::basic_string , std::allocator >’转换为‘char’在赋值和str_iter_test中。C:15:实例化。 ,>

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

第一部分的意思是它不能从字符串转换成char,第二部分表示错误发生在哪个行上,在本例中是字符串s(begin,end);

In this case, your iterators should iterate over char not string:

在这种情况下,迭代器应该遍历char而不是string:

istream_iterator<char> begin(ifile);
istream_iterator<char> end;

#5


1  

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):

您所显示的特定错误消息应该仅是g++发出的错误的一部分。完整的错误应该更接近以下内容(我缩短了路径,这个输出是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:

在我的示例文件t中。cpp:16是下面一行:

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.

如果注释掉这一行(以及后面使用s的那一行),您将发现源代码编译时没有错误。

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.

此时,应该相当清楚的是,您正在不正确地使用std::string构造函数,这意味着您传递给它的参数类型是错误的。你给它一个有std::string(这就是std: basic_string )的东西,它期望有一个有char的东西。

If you consult the documentation for std::string, you'll find that it does have a constructor that looks like:

如果您查阅std::string的文档,您会发现它的构造函数是这样的:

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::strings to the constructor. If you change your istream_iterator<string>s to istream_iterator<char> that should fix the error.

这些输入迭代器允许您从一系列字符创建字符串。然而,您正在向构造函数传递一系列std::string。如果您将istream_iterator s更改为istream_iterator ,应该会修复错误。

#1


6  

This is not the whole error, just a description of one instantiation.

这不是整个错误,只是一个实例化的描述。

Basically you care about:

基本上你关心:

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

1)代码中的哪一行触发了错误(字符串s(开始,结束);

2) what error did it result in (e.g cannot convert 'const std::basic_string<char, std::char_traits<char>, std::allocator<char> >' to 'char' in assignment) and where

2)什么错误导致了(e)。g不能转换'const std::basic_string , std::allocator >' to 'char' in assignment)和where。 ,>

3) you might compare what the compiler is saying the template parameters are, and what you assume them to be.

3)您可以比较编译器所说的模板参数是什么,以及您认为它们是什么。

It probably takes some experience not to be scared of the errors, and it certainly helps to know the library well.

要避免错误,可能需要一定的经验,了解这个库当然很有帮助。

In this case the cause is that the string's constructor is expecting a range of characters, but you are passing a range of strings.

在这种情况下,原因是字符串的构造函数期望字符的范围,但是您正在传递字符串的范围。

#2


5  

Use STLFilt.

使用STLFilt。

#3


3  

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

他有很多更友好的编译器,可以在线查看:http://www.comeaucomputing.com/tryitout/。

for example, this is the 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 constructor expects char iterarator rather than iterator over strings (as in many strings):

这使我假定构造函数期望char迭代器而不是字符串(像许多字符串一样):

istream_iterator<char> begin(ifile);
istream_iterator<char> end;

#4


2  

You actually omitted the key parts of the error message. You get two duplicate 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.

实际上,您省略了错误消息的关键部分。在g++ 4.2中有两个重复的错误。关键部分是:/path/basic_string。错误:不能将“const std:::basic_string , std::allocator >’转换为‘char’在赋值和str_iter_test中。C:15:实例化。 ,>

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

第一部分的意思是它不能从字符串转换成char,第二部分表示错误发生在哪个行上,在本例中是字符串s(begin,end);

In this case, your iterators should iterate over char not string:

在这种情况下,迭代器应该遍历char而不是string:

istream_iterator<char> begin(ifile);
istream_iterator<char> end;

#5


1  

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):

您所显示的特定错误消息应该仅是g++发出的错误的一部分。完整的错误应该更接近以下内容(我缩短了路径,这个输出是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:

在我的示例文件t中。cpp:16是下面一行:

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.

如果注释掉这一行(以及后面使用s的那一行),您将发现源代码编译时没有错误。

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.

此时,应该相当清楚的是,您正在不正确地使用std::string构造函数,这意味着您传递给它的参数类型是错误的。你给它一个有std::string(这就是std: basic_string )的东西,它期望有一个有char的东西。

If you consult the documentation for std::string, you'll find that it does have a constructor that looks like:

如果您查阅std::string的文档,您会发现它的构造函数是这样的:

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::strings to the constructor. If you change your istream_iterator<string>s to istream_iterator<char> that should fix the error.

这些输入迭代器允许您从一系列字符创建字符串。然而,您正在向构造函数传递一系列std::string。如果您将istream_iterator s更改为istream_iterator ,应该会修复错误。