mercredi 20 septembre 2023

How can I systematically work out compiler errors?

I am trying to learn C++ more in depth, and so I've been toying around with various aspects of the language to help solidify my understanding of what is valid syntax and what isn't. Because the code I'm using to learn more about these specific concepts in the language is small and specifically tailored to test out one or two things of interest, I'm typically able to work out compiler errors. And yet, sometimes, even when I know the exact concept at hand that is causing the compiler error, I know that I would never be able to figure it out on my own if the same error came up in a large project.

Here's a good example:

const std::unordered_map<std::string,int> mymap = { {"hello", 1} };

int main()
{
    int num = mymap["hello"];  // error
}

const-unordered-map.cpp:10:28: error: passing ‘const std::unordered_mapstd::__cxx11::basic_string<char, int>’ as ‘this’ argument discards qualifiers [-fpermissive]
10 | int num = mymap["hello"];

aka: Passing a const unordered_map as 'this' argument discards qualifiers

I understand the bracket operator is not valid for a const map, and that a qualifier refers to a cv-qualifier ('const' in this case) and despite that, I still don't really understand how I could systematically approach an error like this one and find a resolution (other than googling)

I want to learn how to get good at working these issues out for myself though. I've searched online for tips in this regard. Typically answers suggest:

  1. Knowing how the build process actually works will help - I am already currently working on this and I think I have a decent overall understanding. Perhaps my inability to understand this error is just indicative of my having a further way to go in this regard
  2. Simply learning over time by being diligent - I intend to do this, I will begin keeping notes on interesting compiler errors and how to understand them

So I'd like to know how you'd go about resolving this error, and if you have any further suggestions for learning the structure/style/jargon of compiler errors in general.

Part 2 of this question is: are some types of errors standardized in any way? I'd imagine that a parser in any C++ compiler would trigger the same compiler errors, but how similar can I expect them to be?

Aucun commentaire:

Enregistrer un commentaire