C++ Error Messages Translated — 10 Common Compilation & Link Errors Explained
Hi everyone! I'm AlanWu, a junior high school C++ learner. When I first started coding, I'd see a wall of red text in the terminal and my brain would just shut down. expected unqualified-id before '}' token — what does that even mean?
After a year of self-learning, I've probably seen every error C++ can throw at a beginner. Here are the 10 most common ones, translated into plain English (and Chinese), why they happen, and how to fix them.
My GitHub: https://github.com/Cn-Alanwu
Error 1: Missing Semicolon
What the compiler says:
error: expected ';' before 'return'
error: expected ';' before '}' token
What it actually means:
You forgot a semicolon at the end of the previous line.
Why it happens:
The compiler doesn't notice the missing semicolon until it hits the next line and sees something that shouldn't be there — like return or }.
int main() {
int x = 5 // ← missing semicolon here
return 0; // compiler complains HERE
}
Fix: Add the semicolon on the line before the one mentioned in the error.
Error 2: Undeclared Identifier
What the compiler says:
error: 'cout' was not declared in this scope
error: 'vector' does not name a type
error: 'myVariable' was not declared in this scope
What it actually means:
You either forgot to #include the right header, or you're misspelled the variable/function name, or you're using a variable before declaring it.
int main() {
cout << "hello"; // forgot #include <iostream> AND std::
return 0;
}
Fix:
- Check you've included the right header (
<iostream>for cout,<vector>for vector) - Check your spelling — C++ is case-sensitive (
MyVariable!=myVariable) - Add
std::orusing namespace std;(but never in a header file!)
Error 3: Unqualified-id Before Token
What the compiler says:
error: expected unqualified-id before '{' token
error: expected unqualified-id before 'if'
What it actually means:
You have a stray character or bracket mismatch — usually an extra } somewhere that closed off something too early.
int main() { // OK
int x = 5;
}} // extra } — the second one broke everything
Fix: Count your curly braces. In VS Code, click on { or } to highlight its match. The error line is often NOT where the real problem is — check lines above it.
Error 4: Undefined Reference
What the compiler/linker says:
undefined reference to 'myFunction()'
undefined reference to 'MyClass::doSomething()'
What it actually means:
You declared a function or promised it exists (in a header or with a forward declaration), but the linker can't find the actual definition anywhere.
// mymath.h
int add(int a, int b); // declaration — I promise this exists
// main.cpp
#include "mymath.h"
int main() {
add(3, 4); // linker: where is add()???
return 0;
}
// Forgot to compile mymath.cpp or forgot to write the definition!
Fix:
- Make sure the
.cppfile containing the function definition is being compiled - Check that the function name and parameters match between declaration and definition
- If you're using a library, make sure you linked it (e.g.,
-lsfml-graphics)
Error 5: Multiple Definition
What the compiler/linker says:
error: multiple definition of 'add(int, int)'
What it actually means:
The same function or variable is defined more than once. Usually happens when you define a function in a header file without inline, and include that header in multiple .cpp files.
// mymath.h — WRONG
int add(int a, int b) {
return a + b;
}
// Every .cpp that includes mymath.h gets its own copy — linker sees duplicates.
Fix:
- Add
inlineto the function:inline int add(...) { ... } - Or move the definition to a
.cppfile and keep only the declaration in the header
Error 6: No Matching Function
What the compiler says:
error: no matching function for call to 'myFunction(int, double)'
note: candidate: 'void myFunction(int, int)'
note: no known conversion from 'double' to 'int'
What it actually means:
You're calling a function with the wrong argument types. The compiler is telling you: "I found a function with a similar name, but the types don't match."
void print(int x, int y) { /* ... */ }
int main() {
print(3, 4.5); // 4.5 is double, not int
return 0;
}
Fix: Read the note: lines carefully — they tell you what the function actually expects. Either convert your argument or write an overload.
Error 7: Segmentation Fault
What the compiler says:
(Nothing at compile time — it crashes at runtime.)
Segmentation fault (core dumped)
What it actually means:
Your program tried to access memory it doesn't own. The most common causes for beginners:
- Accessing an array out of bounds
- Dereferencing a null or dangling pointer
- Accessing an element of an empty vector
std::vector<int> v = {1, 2, 3};
std::cout << v[10]; // out of bounds — segmentation fault (maybe)
int* p = nullptr;
*p = 5; // dereferencing null pointer — definitely segmentation fault
Fix:
- Use
.at()instead of[]with vectors — it throws an exception with a clear message - Always check if a pointer is not null before using it
- Use a debugger to find the exact line that crashed
Error 8: Invalid Use of Incomplete Type
What the compiler says:
error: invalid use of incomplete type 'class MyClass'
What it actually means:
The compiler knows a class exists (because of a forward declaration), but hasn't seen the full definition yet. You can use it as a pointer or reference, but not to access members or create an object.
class B; // forward declaration — compiler knows B exists
class A {
B b; // ERROR: compiler doesn't know how big B is yet
};
Fix:
- If you only need a pointer/reference, use
B* b;orB& b; - If you need the full object,
#include "b.h"instead of forward-declaring
Error 9: Ambiguous Overload
What the compiler says:
error: call of overloaded 'myFunction(int)' is ambiguous
What it actually means:
Multiple functions match your call equally well, and the compiler can't decide which one to use.
void func(int x) { }
void func(long x) { }
void func(float x) { }
int main() {
func(5); // ambiguous: 5 matches int, long, AND float
return 0;
}
Fix:
- Cast your argument to the exact type:
func(static_cast<long>(5)) - Or if you're the one who wrote the overloads, simplify them — too many similar overloads is usually a design issue
Error 10: Stray '\342' '\200' '\234' in Program
What the compiler says:
error: stray '\342' in program
error: stray '\200' in program
error: stray '\234' in program
What it actually means:
You copied code from a website or document, and invisible "smart quotes" (" and ") or other Unicode characters got pasted in. The C++ compiler only understands plain ASCII " and '.
std::cout << "hello"; // ← these are smart quotes — invisible to your eyes but NOT to the compiler
Fix:
- Delete the line and retype the quotes manually
- Paste code into Notepad first, then copy from Notepad into your editor (Notepad strips fancy characters)
- In VS Code, enable "Render Whitespace" to see suspicious characters
Bonus: My Coping Strategy
When I see a huge error log, here's what I do:
- Read only the FIRST error. The rest are often cascading from the first one. Fix the first, recompile, and half of them disappear.
- Go to the line number. The compiler tells you which line triggered the error. Start there.
- Look ABOVE that line. Missing semicolons, unclosed brackets — the actual bug is often on the previous line.
- Search the error text online. I guarantee someone has seen it before.
These 10 errors cover maybe 80% of what you'll encounter in your first year of C++. After that, the compiler stops being scary and starts being helpful.
GitHub: https://github.com/Cn-Alanwu
Tags: #cpp #beginners #tutorial #debugging #programming #coding
C++ 报错信息翻译指南 — 最常见的 10 条编译/链接错误逐条拆解
大家好,我是 AlanWu,一名学 C++ 的初中生。刚开始学的时候,终端里刷出一大片红字,我的大脑直接就宕机了。expected unqualified-id before '}' token — 这到底在说什么?
自学一年,我大概把 C++ 能给新手的所有报错都见了一遍。以下是最常见的 10 条,翻译成人话,解释原因,给出修改方法。
我的 GitHub:https://github.com/Cn-Alanwu
错误 1:少写了分号
编译器说:
error: expected ';' before 'return'
error: expected ';' before '}' token
人话翻译:
你上一行漏写了一个分号。
为什么:
编译器发现分号丢了的时候已经读到下一行了,看到不该出现的 return 或 },才报错。
int main() {
int x = 5 // ← 这里漏了分号
return 0; // 编译器在这里报错
}
修改方法: 在报错行的上一行补上分号。
错误 2:未声明的标识符
编译器说:
error: 'cout' was not declared in this scope
error: 'vector' does not name a type
error: 'myVariable' was not declared in this scope
人话翻译:
要么忘了 #include 对应的头文件,要么打错了变量/函数名,要么没声明就用了。
int main() {
cout << "hello"; // 忘了 #include <iostream>,也忘了 std::
return 0;
}
修改方法:
- 检查有没有引入对应的头文件(cout 需要
<iostream>,vector 需要<vector>) - 检查拼写 — C++ 区分大小写
- 加上
std::或者写using namespace std;(但头文件里千万别用后者)
错误 3:莫名其妙的符号
编译器说:
error: expected unqualified-id before '{' token
error: expected unqualified-id before 'if'
人话翻译:
花括号对不上了 — 通常是多打了一个 },把不该结束的地方提前结束了。
int main() { // OK
int x = 5;
}} // 多了一个 } — 第二个把结构写崩了
修改方法: 数花括号。VS Code 里点击 { 或 } 会高亮它的配对。报错行通常不是 bug 真正所在 — 往上面几行找。
错误 4:未定义的引用
编译器/链接器说:
undefined reference to 'myFunction()'
undefined reference to 'MyClass::doSomething()'
人话翻译:
你在某个地方声明了一个函数(在头文件里,或者用前向声明),编译器信了。但链接的时候,搜遍所有编译好的文件都找不到这个函数的定义。
// mymath.h
int add(int a, int b); // 声明 — "我保证有这个函数"
// main.cpp
#include "mymath.h"
int main() {
add(3, 4); // 链接器:add() 在哪???
return 0;
}
// 忘了编译 mymath.cpp,或者忘了写函数体!
修改方法:
- 确认包含函数定义的
.cpp文件也被编译了 - 检查声明和定义的函数名、参数类型是否完全一致
- 如果用的是第三方库,确认链接命令对了
错误 5:重复定义
编译器/链接器说:
error: multiple definition of 'add(int, int)'
人话翻译:
同一个函数或变量被定义了不止一次。最常见的情况:在头文件里直接写了函数定义(没有 inline),然后多个 .cpp 文件都 include 了这个头文件。
// mymath.h — 错误写法
int add(int a, int b) {
return a + b;
}
// 每个引入 mymath.h 的 .cpp 都会拿到一份副本 — 链接器看到重复
修改方法:
- 给函数加
inline:inline int add(...) { ... } - 或者把定义移到
.cpp,头文件里只保留声明
错误 6:找不到匹配的函数
编译器说:
error: no matching function for call to 'myFunction(int, double)'
note: candidate: 'void myFunction(int, int)'
note: no known conversion from 'double' to 'int'
人话翻译:
你调用函数时传的参数类型不对。编译器在说:"我找到了一个名字类似的,但参数类型对不上。"
void print(int x, int y) { /* ... */ }
int main() {
print(3, 4.5); // 4.5 是 double,不是 int
return 0;
}
修改方法: 认真看 note: 那几行 — 它们精确告诉了你函数实际期望什么类型。要么转换参数类型,要么写一个重载版本。
错误 7:段错误
编译器说:
(编译时不报错,运行时崩溃。)
Segmentation fault (core dumped)
人话翻译:
你的程序试图访问不属于它的内存。新手最常见的三种情况:
- 数组越界
- 解引用空指针或野指针
- 访问空的 vector 的元素
std::vector<int> v = {1, 2, 3};
std::cout << v[10]; // 越界 — 大概率段错误
int* p = nullptr;
*p = 5; // 解引用空指针 — 一定段错误
修改方法:
- 对 vector 用
.at()代替[]— 越界会抛异常,信息更清楚 - 使用指针前先判空
- 用调试器定位到崩溃的具体行
错误 8:不完整类型的使用
编译器说:
error: invalid use of incomplete type 'class MyClass'
人话翻译:
编译器知道某个类存在(比如通过前向声明),但还没看到它的完整定义。你可以用它声明指针和引用,但不能访问成员或创建对象。
class B; // 前向声明 — 告诉编译器 B 存在
class A {
B b; // 报错:编译器不知道 B 有多大
};
修改方法:
- 如果只需要指针或引用,用
B* b;或B& b; - 如果需要完整对象,
#include "b.h"代替前向声明
错误 9:重载歧义
编译器说:
error: call of overloaded 'myFunction(int)' is ambiguous
人话翻译:
有多个函数都能匹配你的调用,编译器不知道选哪个。
void func(int x) { }
void func(long x) { }
void func(float x) { }
int main() {
func(5); // 歧义:5 同时匹配 int、long、float
return 0;
}
修改方法:
- 显式转换参数:
func(static_cast<long>(5)) - 如果你是函数的编写者,考虑简化重载 — 太多相似的重载通常是设计问题
错误 10:Stray 字符
编译器说:
error: stray '\342' in program
error: stray '\200' in program
error: stray '\234' in program
人话翻译:
你从网页或文档里复制代码时,把不可见的"智能引号"(" ")或其他 Unicode 字符也复制进来了。C++ 编译器只认 ASCII 的直引号 " 和 '。
std::cout << "hello"; // ← 这是智能引号 — 肉眼看不出来,编译器看得一清二楚
修改方法:
- 删掉那行,手动敲一遍引号
- 先把代码粘贴到记事本,再从记事本复制到编辑器(记事本会去掉花哨字符)
- VS Code 里打开"渲染空白字符"可以看到可疑的符号
附赠:我的应对策略
每次看到一大片报错,我这样做:
- 只看第一个错误。 后面的往往是连锁反应。修掉第一个,重新编译,一半报错直接消失。
- 跳到对应行号。 编译器告诉了你哪一行触发了错误,先去看那一行。
- 往上一行看。 漏分号、少括号 — 真正的 bug 通常在报错行的上一行。
- 把报错信息复制到网上搜。 我保证你不是第一个遇到的人。
这 10 条错误覆盖了 C++ 第一年可能会遇到的 80%。之后你就会发现:编译器不可怕,它其实是在帮你。
GitHub: https://github.com/Cn-Alanwu
标签: #cpp #beginners #tutorial #debugging #programming #coding
Top comments (0)