TL ; DR:
A
NameErroroccurs when C++ sees a word it does not recognize. Some examples:Misspelling a function name:
cut << "Heyooo!"; // NameError: name 'cut' is not definedMisspelling a variable name:
string animal = "parrot"; cout << anmal; // NameError: name 'anmal' is not definedWhen C++ encounters a name error, it immediately stops executing your code and throws a
NameErrorinstead.
Full lesson:
Humans are prone to making mistakes. Humans are also typically in charge of creating computer programs. To compensate, programming languages attempt to understand and explain mistakes made in their programs.
Here are two common errors that we encounter while writing C++ code:
The syntax error:
Syntax errors are mistakes in the use of the C++ language, and are analogous to spelling or grammar mistakes in a language like English. For example, the sentence "Would you some tea?" does not make sense – it is missing a verb.
Syntax error means there is something wrong with the way your program is written. Some examples:
1. Misuse of commas when declaring a string:
string greeting = 'Hey there!";
When we run this code we'll get an error: missing terminating ' character.
2. Leaving out an important character:
cout < "Hey";
When we run this code we'll get an error: no match for 'operator<'.
The pointer error:
A pointer error occurs when the C++ interpreter sees a word it does not recognize. A common example:
Using a non-existent variable / Misspelling a variable's name:
string animal = "parrot";
cout << aniimal;
When we run this code we'll get an error: 'aniimal' was not declared.
Assignment
Follow the Coding Tutorial and let's practice with errors!
Hint
Look at the examples above if you get stuck.
Free plan: 30 tutor messages. PRO is unlimited.
