C plus plus:FAQ:Errors

From GPWiki
Jump to: navigation, search
40px-sprout.png   This article is a stub. You can help out by expanding it.

There are many sources of errors within C++ code.

Error codes are emitted by the compiler, linker, and runtime libraries to help you debug your program.

Dealing with errors

When a compiler, linker, or other tool discovers an error, they often attempt to continue processing in order to help you identify multiple errors. For example, if the first bug is that an object was used before it was declared, the compiler will assume some default type for the object and attempt to continue procesing the code. Because it cannot know the actual type of the object, it will frequently generate additional errors based on the bad assumption.

Consequently, when you have a long list of errors it is generally best to start at the first error on the list. Correcting it will frequntly eliminate other errors.

Tracking down errors

When you cannot immediately figure out an error, check the documentation and the web.

Use the actual error message generated by the tool. You may need to trim out things that personally identify your code, but you'll get the best results by having as much of the message as possible while still getting a hit.

After searching both the documentaion and the web for help, then it is time to bring your exact error message to IRC or the forums. Be prepared to paste the relavent sections of source code to an external paste site, as directed by others in the discussion.

Compiler errors

The compiler attempts to parse your code and generate an intermediate object file. The compiler will check the syntax of your code to make sure your code makes sense and is legal. Compilers do not verify that the functions actually exist.

Errors at this stage are usually simple changes within your .cpp file.

Examples of these errors include missing semicolons, invalid operations, and missing declarations caused by omitting a header file.

Linker Errors

The linker attempts to find all the functions referenced in the intermediate object file and build them into your executable.

Errors at this stage are generally from omitting an object file or library file from your linker's parameter list.

Examples of these errors include declaring a function in a header but never providing an implementation, and failing to link with an external library.

Runtime Errors

These are also known as 'bugs'.

Runtime errors are logical problems within your program that must be searched out. The language provides some rudimentary tools, such as exceptions, to help figure these out.

Some runtime errors are catastrophic -- they cause the program to crash. Others are more subtle -- they appear to run correctly for a while, yet after operating for a while will provide incorrect results.

Examples of these errors range from crashes that abruptly terminate your program, to blatently incorrect results, to subtle numeric roundoff errors that accumulate over many weeks or even many months of operation. Blatently incorrect results may include an image processing algorithm that appears to generate noise rather than the intended processing. Subtle issues include some missile guidance systems that have been known to drift off course after being allowed to run without acquiring a target for several days.