10 common C++ compiler errors — and what they actually mean

How to read this guide: find your error message, jump to its entry, fix it. General rule first: fix only the FIRST error, then recompile — one missing semicolon can produce ten cascade errors, and errors two through ten usually vanish.

1. error: expected ';' before '}' token

Meaning: a statement above is missing its semicolon. The compiler often reports it a line after the real mistake.

int x = 5      // ← missing ';' here
cout << x;

Fix: add the semicolon. When you see this error, check the end of the previous line first.

2. 'cout' was not declared in this scope

Meaning: the compiler has no idea what cout is — usually because <iostream> isn't included, or using namespace std; is missing (without it, write std::cout).

#include <iostream>        // without this line: error
using namespace std;       // without this: 'cout' unknown

int main() {
    cout << "hi" << endl;
}

3. 'x' was not declared in this scope

Meaning: you used a name the compiler hasn't seen. Two usual causes: a typo (cont vs count — C++ is case-sensitive too: Count ≠ count), or using a variable before declaring it.

int main() {
    x = 5;        // ← error: nothing named x exists yet
    int x;
}

4. fatal error: iostrem: No such file or directory

Meaning: an #include name is misspelled. The compiler quotes the exact name it couldn't find — read it character by character.

#include <iostrem>   // ← typo: should be <iostream>

5. error: invalid conversion from 'const char*' to 'char'

Meaning: single quotes vs double quotes. 'a' is one character; "a" is a string. Mixing them where the other was expected produces this error (or a cousin of it).

char c = "a";   // ← "a" is a string; use 'a'
string s = 'a'; // ← 'a' is a char; use "a"

6. error: expected '}' at end of input

Meaning: a closing brace is missing somewhere. The compiler hit end-of-file still waiting for it — the mistake is usually many lines up, at the opening of a block.

Fix: count braces, or let your editor highlight matching pairs. Indenting honestly makes mismatches visible.

7. undefined reference to 'main' — a LINKER error

Meaning: note the new word: linker, not compiler. Your code compiled, but there's no entry point — usually main is misspelled (Main, mian) or missing. See what int main() is.

int Main() {   // ← capital M: this is just a random function
    return 0;
}

8. undefined reference to 'sayHi()' — also linker

Meaning: the function was declared (promised) but never defined (delivered), or its .cpp file wasn't compiled/linked.

void sayHi();          // declaration: "it exists, trust me"

int main() {
    sayHi();           // linker: "where is it?!" — body was never written
}

9. unresolved external symbol "sayHi" (Visual Studio)

Meaning: exactly the same linker problem as #8 — Microsoft's compiler just uses different words. Learning both spellings doubles the error messages you can decode from search results.

10. warning: lines — the ones beginners skip (don't)

Meaning: a warning says "this compiles, but it smells wrong": unused variables, uninitialized values, implicit conversions that lose data. Warnings don't stop the build — which is exactly why people miss real bugs hiding behind them. Treat every warning as a bug you haven't met yet.

A program that compiles clean

The corrected version of the classics above — run it as a sanity check after fixing your own error:

#include <iostream>
using namespace std;

void sayHi() {
    cout << "hi!" << endl;
}

int main() {
    int x = 5;        // declared before use
    char c = 'a';     // char with single quotes
    cout << x << " " << c << endl;
    sayHi();
    return 0;
}

The general debugging routine

  1. Read the first error only. Fix it. Recompile. (Errors cascade — the rest are often echoes.)
  2. Check the line the compiler names, then the line above it. Off-by-one-line reporting is normal.
  3. Compiler error vs linker error: "error:" means the language rules; "undefined reference / unresolved external" means a promised function was never delivered.
  4. Searching the exact message works — drop your own file paths and variable names first, keep the boilerplate part of the message.

Related: what int main() is (error #7 explained deeper), cin vs getline (runtime input bugs, the other half of beginner pain), and the Hello World lesson where error #1 makes a cameo.