[2026] C++ Compile Errors Complete Guide | 10 Common Errors for Beginners
이 글의 핵심
Complete guide to 10 common C++ compile errors for beginners. undefined reference, segmentation fault, header redefinition solutions
🎯 What You’ll Learn (Reading Time: 25 minutes)
TL;DR: Master 10 common C++ compile errors and their solutions. From undefined reference to segmentation fault, gain practical debugging skills. What you’ll learn:
- ✅ Understand causes and solutions for 10 common C++ compile errors
- ✅ Master error message reading and debugging strategies
- ✅ Improve practical code problem-solving abilities Practical Applications:
- 🔥 Fast debugging (error message interpretation)
- 🔥 Code quality improvement (prevent common mistakes)
- 🔥 Development productivity boost (reduce error resolution time) Difficulty: Beginner | Practice Examples: 10 | Immediately Applicable
Introduction: “I don’t understand what the error message means”
The most frustrating moment when first learning C++ is encountering compile error messages. Error messages are long, complex, and difficult to understand what the problem is. This article summarizes 10 common C++ compile errors for beginners and immediately applicable solutions. Each error includes cause, error message, solution, and practical examples.
1. undefined reference to ‘function’
Cause
Occurs when a function declaration exists but the definition (implementation) is missing, or when the linker cannot find the implementation.
Error Message
undefined reference to `myFunction()'
Common Situations
- Only declaration in header, no implementation in .cpp
- Corresponding .o file not included during linking
- Template function defined in .cpp (templates must be defined in headers)
Solution
// ❌ Wrong: Only declaration, no definition
// header.h
void myFunction();
// main.cpp
#include "header.h"
int main() {
myFunction(); // undefined reference!
}
// ✅ Correct: Add definition
// header.h
void myFunction();
// impl.cpp
#include "header.h"
void myFunction() {
// implementation
}
// Include all .cpp files during compilation
// g++ main.cpp impl.cpp -o program
For Template Functions
// ❌ Wrong: Template defined in .cpp
// header.h
template<typename T>
void process(T value);
// impl.cpp
template<typename T>
void process(T value) {
// implementation
}
// ✅ Correct: Template defined in header
// header.h
template<typename T>
void process(T value) {
// implementation (write in header)
}
2. segmentation fault (core dumped)
Cause
Program crashes due to invalid memory access. Mainly caused by nullptr dereferencing, array bounds exceeded, use-after-free, etc.
Error Message
Segmentation fault (core dumped)
Common Situations
- nullptr dereferencing
- Array bounds exceeded
- Use after delete (dangling pointer)
- Stack overflow (infinite recursion)
Solution
// ❌ nullptr dereferencing
int* ptr = nullptr;
*ptr = 10; // segmentation fault!
// ✅ nullptr check
int* ptr = nullptr;
if (ptr != nullptr) {
*ptr = 10;
}
// ❌ Array bounds exceeded
int arr[5];
arr[10] = 100; // segmentation fault!
// ✅ Bounds check or use vector
std::vector<int> vec(5);
vec.at(10) = 100; // throws exception (safe)
// ❌ Use after delete
int* ptr = new int(10);
delete ptr;
*ptr = 20; // segmentation fault!
// ✅ Assign nullptr after delete
int* ptr = new int(10);
delete ptr;
ptr = nullptr;
if (ptr != nullptr) {
*ptr = 20;
}
Debugging Tip
# Debug with gdb
g++ -g program.cpp -o program
gdb ./program
(gdb) run
# Check crash point
(gdb) backtrace
3. error: redefinition of ‘class/function’
Cause
Occurs when the same class or function is defined multiple times. Mainly appears when header files are included multiple times.
Error Message
error: redefinition of 'class MyClass'
error: redefinition of 'void myFunction()'
Common Situations
- No include guard in header file
- Function definition in header (when not inline)
Solution
// ❌ No include guard
// header.h
class MyClass {
// ...
};
// ✅ Use include guard
// header.h
#ifndef HEADER_H
#define HEADER_H
class MyClass {
// ...
};
#endif
// Or use #pragma once (simpler)
// header.h
#pragma once
class MyClass {
// ...
};
For Function Definitions
// ❌ Function definition in header (duplicates when included in multiple .cpp)
// header.h
void myFunction() {
// implementation
}
// ✅ Only declaration in header, definition in .cpp
// header.h
void myFunction();
// impl.cpp
void myFunction() {
// implementation
}
// Or use inline
// header.h
inline void myFunction() {
// implementation
}
4. error: no matching function for call
Cause
Occurs when argument types or count don’t match during function call.
Error Message
error: no matching function for call to 'myFunction(int)'
note: candidate: void myFunction(double)
Common Situations
- Argument type mismatch
- Argument count mismatch
- const mismatch
Solution
// ❌ Argument type mismatch
void process(double value) {
// ...
}
int main() {
std::string str = "hello";
process(str); // error: no matching function!
}
// ✅ Match types
void process(const std::string& value) {
// ...
}
int main() {
std::string str = "hello";
process(str); // OK
}
// ❌ const mismatch
void print(std::string& str) {
// ...
}
int main() {
const std::string str = "hello";
print(str); // error: no matching function!
}
// ✅ Add const
void print(const std::string& str) {
// ...
}
5. error: ‘identifier’ was not declared in this scope
Cause
Occurs when using a variable or function without declaring it.
Error Message
error: 'myVariable' was not declared in this scope
error: 'myFunction' was not declared in this scope
Common Situations
- Header file not included
- Namespace not specified
- Using variable before declaration
Solution
// ❌ Header not included
int main() {
std::cout << "Hello\n"; // error: 'cout' was not declared!
}
// ✅ Include header
#include <iostream>
int main() {
std::cout << "Hello\n"; // OK
}
// ❌ Namespace not specified
#include <vector>
int main() {
vector<int> vec; // error: 'vector' was not declared!
}
// ✅ Add std:: or using namespace
#include <vector>
int main() {
std::vector<int> vec; // OK
}
// ❌ Using variable before declaration
int main() {
x = 10; // error: 'x' was not declared!
int x;
}
// ✅ Use after declaration
int main() {
int x;
x = 10; // OK
}
6. warning: implicit conversion loses integer precision
Cause
Warning occurs when there is potential data loss during type conversion.
Error Message
warning: implicit conversion loses integer precision: 'long' to 'int'
Common Situations
- Large type → small type conversion
- size_t → int conversion
Solution
// ❌ Implicit conversion (warning)
long bigNumber = 1000000000L;
int smallNumber = bigNumber; // warning!
// ✅ Explicit casting
long bigNumber = 1000000000L;
int smallNumber = static_cast<int>(bigNumber);
// ❌ size_t → int conversion
std::vector<int> vec = {1, 2, 3};
int size = vec.size(); // warning!
// ✅ Use size_t or explicit casting
std::vector<int> vec = {1, 2, 3};
size_t size = vec.size(); // OK
// Or
int size = static_cast<int>(vec.size());
7. error: use of deleted function
Cause
Occurs when trying to copy an object whose copy constructor or assignment operator is deleted.
Error Message
error: use of deleted function 'MyClass::MyClass(const MyClass&)'
Common Situations
- Copying unique_ptr
- Class with copy constructor = delete
Solution
// ❌ Copying unique_ptr
std::unique_ptr<int> ptr1 = std::make_unique<int>(10);
std::unique_ptr<int> ptr2 = ptr1; // error: use of deleted function!
// ✅ Use move
std::unique_ptr<int> ptr1 = std::make_unique<int>(10);
std::unique_ptr<int> ptr2 = std::move(ptr1); // OK
// ❌ Class with deleted copy constructor
class NonCopyable {
public:
NonCopyable() = default;
NonCopyable(const NonCopyable&) = delete;
};
NonCopyable obj1;
NonCopyable obj2 = obj1; // error: use of deleted function!
// ✅ Use reference or pointer
NonCopyable obj1;
NonCopyable& obj2 = obj1; // OK (reference)
8. error: expected ’;’ before ’}‘
Cause
Occurs when there are syntax errors such as missing semicolon.
Error Message
error: expected ';' before '}'
Common Situations
- Missing semicolon at end of class definition
- Missing semicolon at end of variable declaration
Solution
// ❌ Missing semicolon at end of class definition
class MyClass {
int value;
} // error: expected ';'!
// ✅ Add semicolon
class MyClass {
int value;
}; // OK
// ❌ Missing semicolon at end of variable declaration
int main() {
int x = 10
int y = 20; // error: expected ';'!
}
// ✅ Add semicolon
int main() {
int x = 10;
int y = 20; // OK
}
9. error: invalid use of incomplete type
Cause
Occurs when trying to use a class that only has forward declaration without definition.
Error Message
error: invalid use of incomplete type 'class MyClass'
Common Situations
- Only forward declaration, header not included
- Circular dependency
Solution
// ❌ Only forward declaration, then use
class MyClass; // forward declaration
int main() {
MyClass obj; // error: invalid use of incomplete type!
obj.doSomething();
}
// ✅ Include header
#include "MyClass.h"
int main() {
MyClass obj; // OK
obj.doSomething();
}
// Solving circular dependency
// A.h
#pragma once
class B; // forward declaration
class A {
B* b; // pointer is OK
};
// B.h
#pragma once
class A; // forward declaration
class B {
A* a; // pointer is OK
};
10. error: no member named ‘X’ in ‘Y’
Cause
Occurs when trying to access a non-existent member variable or member function.
Error Message
error: no member named 'getValue' in 'MyClass'
Common Situations
- Typo
- Accessing private member
- Accessing non-inherited member
Solution
// ❌ Typo
class MyClass {
public:
int getValue() const { return value; }
private:
int value;
};
int main() {
MyClass obj;
obj.getvalue(); // error: no member named 'getvalue'! (typo)
}
// ✅ Correct name
int main() {
MyClass obj;
obj.getValue(); // OK
}
// ❌ Accessing private member
class MyClass {
private:
int value;
};
int main() {
MyClass obj;
obj.value = 10; // error: 'value' is private!
}
// ✅ Use public getter/setter
class MyClass {
public:
void setValue(int v) { value = v; }
int getValue() const { return value; }
private:
int value;
};
int main() {
MyClass obj;
obj.setValue(10); // OK
}
How to Read Error Messages
C++ compiler error messages are long and complex, but once you know the pattern, they’re easy to read.
Error Message Structure
filename:line:column: error: error type: detailed description
Example
main.cpp:10:5: error: 'x' was not declared in this scope
x = 10;
^
How to read:
- Check filename and line number:
main.cpp:10→ line 10 of main.cpp - Check error type:
'x' was not declared in this scope→ x not declared - Check code:
x = 10;→ using x without declaration
When Multiple Errors Occur in Chain
Solve from the first error. Fixing the first error often makes the rest disappear automatically.
Debugging Strategies
1. Enable Compiler Warnings
# Enable all warnings
g++ -Wall -Wextra -Werror main.cpp -o program
# -Wall: basic warnings
# -Wextra: additional warnings
# -Werror: treat warnings as errors
2. Include Debug Symbols
# Include debug information
g++ -g main.cpp -o program
# Debug with gdb
gdb ./program
3. Step-by-Step Debugging
- Solve in order: Compile errors → Link errors → Runtime errors
- Solve one error at a time
- Google the error message (use Stack Overflow)
4. Simplify Code
When errors occur:
- Create minimal reproducible code for testing
- Simplify complex code to identify the problem
- Comment out line by line to find the problem point
Mistake Prevention Checklist
Check Before Compiling
- Add include guard or
#pragma onceto all header files - Confirm both function declaration and definition exist
- Define template functions in headers
- Declare all variables before use
- Check nullptr before dereferencing
Coding Habits
- Always use
nullptr(instead of NULL, 0) - Use range-based for (prevent index errors)
- Use smart pointers (prevent memory leaks)
- Use const actively (prevent unintended modifications)
- Don’t ignore compiler warnings
Conclusion
C++ compile errors are difficult at first, but once you learn the patterns, you can solve them quickly. The 10 errors covered in this article are the most common problems for C++ beginners. Key Summary:
- Read error messages accurately (filename, line number, error type)
- Solve from the first error (prevent chain errors)
- Enable compiler warnings (-Wall -Wextra)
- Habituate nullptr checks, bounds checks
- Use include guards (prevent header duplication) Next Steps:
- C++ Debugging Tools Guide (gdb, valgrind usage)
- C++ Memory Error Debugging (memory leaks, dangling pointers)
- C++ Code Quality Improvement (static analysis, linters)