简体   繁体   中英

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? 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*. 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. 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? 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.

The error message you provided seems to be heavily misquoted (or mangled by the parser). GCC produces the following

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. 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. The error message gives you the full name of that data template function

     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. Hence it has no type. However, apparently the inner workings of GCC compiler treat that combination as an expression of member function type. 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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