Common Visual C++ Syntax Errors


Introduction

High level languages such as C++, Pascal, Fortran, etc, have a rigid set of syntax rules or grammer. It is very common for both experienced and inexperienced programmers to write programs that contain errors in syntax (just like web authors misspell words). Human beings can easily handle a misspelled word while reading a document. Unfortunately, the compiler is not so intelligent, and is very sensitive to syntax errors. Consequently, the slightest error in your C++ program can cause enormous lists of debug messages. When you compile a program and encounter such a large list of errors, you should not panic. Frequently, you are closer to a syntactically correct program than it seems.

This excercise is designed to give you some initial experience with troubleshooting the strange debug messages produced by the compiler. You will purposefully introduce errors into Hello World and note what debug message is produced for each error. An experienced programmer will hopefully be able to read a debug message, and quickly deduce the error that caused it. With practice, you will be doing the same.

Getting Started

If you haven't done so already, complete the "Getting started with Visual C++" tutorial. This excercise assumes that you have created, compiled, ran, and saved the program that is developed in that tutorial. This program simply prints the sentence Hello, World! on the screen.

An Example

To produce an example of a syntax error, delete the line of the program which says #include <iostream.h>, and compile the program. Don't worry about what this line does, or why it is needed, you will learn that in class soon enough. Now click on the Compile Build. Your screen should look like the following:

Notice that there are three debug messages listed in the window labelled 'Build'. The messages include the name of the file, the line number where the error was discovered (which is not necessarily the line that has the error), the error number (C2065, for example) and text describing the message. The text is what you will be primarily concerned with, and it is what is meant when we say 'debug messages' in this excercise. The debug messages for this error are:

  1. 'cout' : undeclared identifier
  2. '<<' : bad right operand
  3. 'endl' : undeclared identifier
All three of these errors are caused by the one missing line. As mentioned above, we are primarily concerned with just the first error. If you double click on the first error, it will cause Visual C++ to highlight the line with that error. Notice that this highlighted line is not the line that you removed. Although the cause of the error is the missing line, the compiler didn't detect an error until it got to a later line. For now, you should remember that if you have a debug message that states that 'cout' is an undeclared identifier, it could be because the program is missing a line of code which says #include <iostream.h>.

Now that you have looked at this debug message, re-enter the line and compile the program, it should now compile without any error messages.

The Excercise

The following is a list of about 20 seperate errors to introduce into the Hello World program. You are to record the first debug message that Visual C++ generates when you attempt to compile Hello World with that error.  Note that a single error (such as forgetting a semicolon) can generate a large list of debug messages. The most important debug message (the one you should pay attention to) is the first one. Also, pay attention to whether the debug message refers to the line that you changed, the line below it, or a completely different line.

After each change that you make, compile the program, record the first error message, and then undo your change before proceeding to the next error

  1. Make the following changes to the line which says: #include <iostream.h>
  2. Make the following changes to the line that says: int main()
  3. Make the following change to the line that says: {
  4. Make the following changes to the line that says: cout << "Hello, World!" << endl;
  5. Make the following changes to the line that says: return 0;
  6. Make the following change to the line that says: }
  7. Create another project, called win32. Rather than making this project a Win32 Console Application, make it a Win32 Application. Enter in your code from the Hello World program, and compile the program. What error message do you get? Remember, always create your projects as Win32 Console Applications.

Last modified Jan 18, 1998 - colbster@cs.wisc.edu