2 Common Errors in C++ during the Build Step - dummies

By Stephen R. Davis

When you’re entering a program in C++, the Build step almost certainly does not come off without error. A Gold Star program is one that works the first time you build and execute it. You’ll almost never write a Gold Star program in your entire programming career. Nobody does. Don’t sweat it.

These are just two of the myriad ways to screw up in C++. Learning how to interpret what the compiler is trying to tell you — with its error and warning messages — is an important part of learning the language. It can come only from many months of practice and gaining experience with the language. Hopefully, these two examples will get you jump-started.

Misspelled commands in C++

Misspelled commands are the easiest errors to identify and correct. To demonstrate the point, an extra t has been added to this line of code so that it reads

intt celsius;

Unlike int, the word intt has no meaning to C++. Building the resulting program with this line generated the display shown.


Notice first the small, red block on Line 14 that indicates a problem somewhere on this line. You can read all about it down in the Build Messages tab in the lower-right window. Here you can see the following messages:

14 error: 'intt' was not declared in this scope
14 error: expected ';' before 'celsius'
16 error: 'celsius' was not declared in this scope

The first line indicates the name of the function that contains the error. It’s easy to believe that all of the code in this program is in a function called main. The next line is the key. This says essentially that C++ didn’t understand what intt is on line 14 of the program.

The error message is a bit cryptic, but suffice it to say you’ll get this same error message almost every time you misspell something. The remaining error messages are just by-products of the original error.

One C++ error can generate a cascade of error messages. It’s possible to identify and fix multiple errors in a single build attempt, but it takes experience to figure out which errors stem from which others. For now, focus on the first error message. Fix it and rebuild the program.

Missing semicolon in C++ code

Another common error is to leave off a semicolon. The message that this error generates can be a little confusing. To demonstrate, remove the semicolon from the declaration on line 14 so that it reads

int celsius
cout << "Enter the temperature in Celsius:";

The error reported by C++ for this offense points not to line 14 but to the following line, 15:

15 error: expected initialization before 'cout'
16 error: 'celsius' was not declared in this scope

This is easier to understand when you consider that C++ considers newlines as just another form of whitespace. Without the semicolon, C++ runs the two lines together. There is no separate line 14 anymore. C++ can interpret the first part, but it doesn’t understand the run-on sentence that starts with cout.

Missing semicolons often generate error messages that bear little resemblance to the actual error message, and they’re almost always on the next line after the actual error. If you suspect a missing semicolon, start on the line with the reported error and scan backward.