There are many types of programming errors. This tutorial discusses the general categories under which those errors fall:
Note that the error messages shown below may be specific to our compiler/linker or machines. Nonetheless, other systems and compilers will provide similar information.
cin) are put together to form an executable. Often,
compiling and linking together are just referred to as compiling.
There are two severities of messages the compiler can give:
A compiler warning indicates you've done something bad, but not something that will prevent the code from being compiled.
You should fix whatever causes warnings since they often lead to other problems that will not be so easy to find.
Example: Your code calls the pow() (raise to a
power) library function, but you forgot to include math.h.
Because you've supplied no prototype for the pow()
function (its in math.h), the compiler warns you that it
assumes pow() returns an int and that it
assumes nothing about pow()'s parameters:
somefile.cpp:6: warning: implicit declaration of function `int pow(...)'
This is a problem since pow() actually returns a
double. In addition, the compiler can't type-check (and
possibly convert) values passed to pow() if it doesn't know
how many and what type those parameters are supposed to be.
A compiler error indicates something that must be fixed before the code can be compiled.
Example: You forget a semi-colon (;) at the end
of a statement and the compiler reports:
somefile.cpp:24: parse error before `something'
Compiler messages usually list the file and line number where a problem occurs. Nonetheless, errors often occur on the lines prior to what the error message lists. Especially check the line immediately preceding where the error message indicates.
Finally, note that some compilers may choose to call something an error while others may just call it a warning or not complain at all.
Example 1: You misspell the name of a function (or method) when you declare, define or call it:
void Foo();
int main()
{
Foo();
return 0;
}
void foo()
{
// do something
}
so that the linker complains:
somefile.o(address): undefined reference to `Foo(void)'
that it can't find it.
Example 2: You use the X Windows XDrawLine()
function (and include the header file for it), but forget to use the
-lX11 option to tell the linker to use the X Windows
library. It will complain that it doesn't know about the
XDrawLine() function:
somefile.o(address): undefined reference to `XDrawLine'
A fatal error is basically when the executable crashes.
Example 1: The program divided by zero, as in:
int scores = 500; int num = 0; int avg; avg = scores / num;
The program would crash saying:
Floating exception
Example 2: Segmentation faults, Bus errors.
These occur when you try to access memory that your program is not allowed to use or that doesn't exist in the computer (i.e., there is only so much memory in the computer).
Your program will crash giving the "Segmentation fault" or "Bus error" message.
These errors often occur due to improper use of arrays or pointers.
Example: Using an uninitialized array index...
may cause such an error. These, particularly, are tricky since they may or may not occur based on what the initial garbage value of the index is when you run the program. Remember, you cannot generally assume variables get initialized to zero.int values[10]; int i; cout << "The ith value is: " << values[i] << endl;
A logic error occurs when your program simply doesn't do what you want it to.
Example: You have an infinite loop because you did not update the variable(s) used in the condition of a loop, as in:
cin >> account_num;
Assume user did not enter -1.
while (account_num != -1) {
cout << "Account #: " << account_num << endl;
ProcessAccount(account_num);
// Oops...Forgot to read another account # here!
}
Both techniques can be applied either with or without a debugging utility.