Designing Reusable Code   «Prev  Next»

Designing Reusable C++ Code Conclusion

Lesson 5

How to use a C++ compiler?

This course explains the differences in C++ compilers.

The code in this course is written in ANSI C++ and should compile without errors on many ANSI-compliant compilers. ANSI C++, however, is a rapidly evolving standard and many older compilers are not able to use the latest additions in the language.
For this reason, the code used in this course was written to run on as wide a variety of compilers as possible.

Debugging C++ Programs

The process of finding such errors is called debugging the program. Syntax errors occur when the source code violates the syntax (i.e., the grammar rules) of the language. As the compiler translates code into machine language, it checks whether the source code it is translating conforms to the syntax rules of the language. If any of these rules is violated, the compiler generates an error message that explains the (apparent) problem. For example, if we forgot to type the semicolon at the end of a statement in the tenth line of some program and entered
double amount
cin >> amount;
instead of
double amount;
cin >> amount;

the compiler might display a diagnostic message like the following:
Error: ';' expected
finAidProgram.cpp line 10
A different compiler might display a less precise diagnostic for the same error, such as
finAidProgram.cpp: In function 'int maine)':
finAidProgram.cpp:11: parse error before '>'

This compiler displayed the number of the line it was processing when it detected that something was wrong, which is the line following the line containing the error.

The code has been tested using
  1. GNU gcc/g++ (Unix),
  2. Visual Studio Code 2017, and
If you are using another compiler, though, you may need to modify the code slightly to get it to compile.
You may have to rely on the compiler vendor's support if compiling errors occur.
Key differences might exist in various compiler flags and library files. Keep this in mind when working through this course.