Debugging: Reading Compiler Errors and Fixing Bugs
Stay calm, read the message, and fix bugs one small step at a time
Big Idea: Every programmer writes bugs -- even the experts do, every single day. A bug is just a mistake in your code. The good news is that C++ usually tells you something is wrong. Learning to read those messages calmly, instead of panicking, is one of the most useful skills you can build.
By the end of this topic you will be able to:
- tell apart compile-time errors, linker errors, and runtime bugs,
- read a compiler message and find the file, line, and reason,
- understand common messages like
expected ';',undeclared identifier, andno matching function, - follow a calm, step-by-step routine to find and fix a bug.
1. From source code to a running program
Before we hunt bugs, let us see the journey your program takes. When you press "Run", the computer does three jobs in order:
- Compile -- the compiler reads your
.cppfile and turns it into machine instructions. If your text breaks the rules of C++ (a missing semicolon, a misspelled name), it stops here with a compile-time error. - Link -- the linker joins all the pieces together (your code plus the library code) into one runnable file. If a function was promised but never actually written, you get a linker error.
- Run -- the finished program actually executes. If it starts but does the wrong thing, or crashes halfway, that is a runtime bug.
Knowing which of these three stages failed already tells you a lot about where to look.