简体   繁体   English

我如何解释这个C ++错误

[英]How can I interpret this C++ error

I have this line of code (line is of type string): 我有这行代码(行是字符串类型):

char* p = line.data;

Before you respond, I already figured out what I was doing wrong, I needed some parentheses at the end. 在您做出回应之前,我已经弄清楚我做错了什么,最后需要加括号。 What I want to ask you is how I am supposed to interpret the error I got for future reference: 我想问你的是我应该如何解释我得到的错误,以备将来参考:

error: cannot convert 'std::basic_string<_CharT, _Traits, _Alloc>::data<char, std::char_traits<char>, std::allocator<char> >' from type 'const char* (std::basic_string<char>::)()const noexcept (true)' to type 'char*'

so first I must ask, could this possibly be more confusing? 所以首先我必须问,这可能会更令人困惑吗? How on earth do you C++ developer's cope with stuff like this? C ++开发人员如何应对这样的事情? Thank heaven for higher level languages right? 感谢天堂提供的高级语言,对吗? OK so all of those questions were just rhetorical. 好的,所以所有这些问题都只是口头上的。

OK so I have read this error about a dozen times now. 好,所以我现在已经读了大约十二次此错误。 I also reviewed the string data method documentation, and it does say that it returns a char*. 我还查看了字符串数据方法文档,它确实说它返回一个char *。 So I am confused about the following: 因此,我对以下内容感到困惑:

  1. Referring to this: 引用此:

     basic_string<_CharT, _Traits, _Alloc> 

According to the basic_string documentation, it has one template parameter, but this error seems to be saying there are three: _CharT, _Traits, and _Alloc. 根据basic_string文档,它具有一个模板参数,但是此错误似乎是在说三个:_CharT,_Traits和_Alloc。 Why does this basic string have three template parameters when the documentation says it has one? 为什么说文档中有一个基本字符串有三个模板参数? I guess I don't understand how to interpret that documentation or that error message. 我想我不明白如何解释该文档或该错误消息。

  1. Then it looks like the data portion also has template parameters, 看起来数据部分也有模板参数,

     ...::data<char, std::char_traits<char>, std::allocator<char> > 

but according to the 'data' method documentation, it is defined like this: 但根据“数据”方法文档,其定义如下:

    const char* data() const noexcept;

I don't see any template parameters in that definition. 我在该定义中看不到任何模板参数。 Why does the error message show all of these template parameters? 为什么错误消息显示所有这些模板参数?

  1. Then it gets more confusing, the error message says it's trying to cast from type: 然后变得更加混乱,错误消息说它正在尝试从类型强制转换:

     const char* (std::basic_string<char>::)()const noexcept (true) 

so I think this is C++'s way of describing a function type, is that right? 所以我认为这是C ++描述函数类型的方式,对吗? Why does it end with the word true in parentheses? 为什么在括号中以真这个词结尾?

So I guess I'm mainly trying to figure out why the C++ exceptions have all of these bizarre template parameters that I don't see in any documentation. 所以我想我主要是想弄清楚为什么C ++异常具有所有这些奇怪的模板参数,而我在任何文档中都没有看到这些参数。

The error message you provided seems to be heavily misquoted (or mangled by the parser). 您提供的错误消息似乎被严重错误引用(或被解析器破坏)。 GCC produces the following GCC产生以下内容

error: cannot convert ‘std::basic_string<_CharT, _Traits, _Alloc>::data<char, std::char_traits<char>, std::allocator<char> >’ from type ‘const char* (std::basic_string<char>::)()const’ to type ‘char*’
  1. I don't know what documentation told you that std::basic_string has only one template parameter. 我不知道什么文档告诉您std::basic_string只有一个模板参数。 It has three. 它有三个。

  2. The class itself and the member function of the class are independent templates. 类本身和类的成员函数是独立的模板。 So basic_string is a template class and member function basic_string::data() is a separate template function. 因此basic_string是模板类,成员函数basic_string::data()是单独的模板函数。 The error message gives you the full name of that data template function 该错误消息为您提供了该data模板功能的全名

     std::basic_string<_CharT, _Traits, _Alloc>::data<char, std::char_traits<char>, std::allocator<char> > 

    The first part 第一部分

     std::basic_string<_CharT, _Traits, _Alloc> 

    is just the class name, while the rest is the template function with the actual template arguments listed after the function name. 只是类名,其余的是模板函数,其实际模板参数列在函数名称后。 This is in no way a perfect way to refer to this template. 绝对不是引用此模板的完美方法。 Most likely it is somehow derived from the inner workings of the compiler. 它很可能是从某种程度上从编译器的内部工作派生的。

  3. The "cast from function type" part has no logic in it from the language point of view. 从语言的角度来看,“从函数类型广播”部分没有逻辑。 From the point of view of C++ language, line.data is not a valid expression at all. 从C ++语言的角度来看, line.data根本不是有效的表达式。 Hence it has no type. 因此,它没有类型。 However, apparently the inner workings of GCC compiler treat that combination as an expression of member function type. 但是,显然GCC编译器的内部工作将该组合视为成员函数类型的一种表达。 The error message actually shows that type as 错误消息实际上将该类型显示为

     const char* (std::basic_string<char>::)()const 

    Other compilers are also known to do that. 其他编译器也可以做到这一点。 Again, this is just inner mechanics of the compiler showing through. 同样,这只是编译器的内部机制。

In practical terms, the way C++ programmers deal with this is by making the same error repeatedly enough times that they can generally recognise the "shape" of the error message rather than having to read it in detail. 实际上,C ++程序员处理此错误的方式是重复多次重复同一错误,以使他们通常可以识别错误消息的“形状”,而不必详细阅读。

Usually what I do when I get an error on a source line, is look to see whether I've done something dumb (without reading the error message in detail). 通常,当我在源代码行上出现错误时,我会做的是看我是否做过一些愚蠢的事情(无需详细阅读错误消息)。 Only if that fails and I can't see it, then I read the error message and see what the compiler is complaining about. 仅当该操作失败并且看不到它时,我才会读取错误消息,并查看编译器在抱怨什么。 Sometimes it's still inscrutable and I have to go back to looking for something dumb. 有时它仍然难以理解,我不得不回头寻找一些愚蠢的东西。 In extreme cases this might happen a few times before I figure it out. 在极端情况下,在我弄清楚之前,可能会发生几次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM