Ready to practice?
Sign up to access interactive coding exercises and track your progress.
C++ Error Reference
A comprehensive reference guide for common C++ errors. Use this as a quick lookup when you encounter unfamiliar error messages.
Prerequisites
Error Reference
This lesson serves as a comprehensive reference for common C++ errors you'll encounter when working with variables, data types, operators, expressions, and input/output. Use this as a quick lookup guide when you encounter unfamiliar error messages.
Don't try to memorize all these errors! Bookmark this lesson and return to it when you encounter unfamiliar error messages. Use Ctrl+F (or Cmd+F on Mac) to search for keywords from your error message to quickly find relevant examples and solutions.
Variable Declaration and Initialization Errors
1. Using Uninitialized Variables
Problematic code:
#include <iostream>
int main() {
int count;
std::cout << count + 5 << std::endl; // Using uninitialized variable
return 0;
}
Error message (varies by compiler):
warning: variable 'count' is uninitialized when used here
Why this happens: Local variables aren't automatically initialized to zero - they contain garbage values.
Fix: Always initialize variables when you declare them:
int count{}; // Modern initialization - defaults to 0
// or
int count = 0; // Traditional initialization
2. Variable Name Conflicts
Problematic code:
#include <iostream>
int main() {
int age{25};
int age{30}; // Redeclaring the same variable
return 0;
}
Error message:
error: redeclaration of 'int age'
note: 'int age' previously declared here
Fix: Use different variable names or update the existing variable:
int age{25};
age = 30; // Assignment, not declaration
3. Invalid Variable Names
Problematic code:
#include <iostream>
int main() {
int 2users = 10; // Can't start with number
int user-name = 5; // Hyphens not allowed
int class = 8; // 'class' is a reserved keyword
return 0;
}
Error messages:
error: expected primary-expression before 'int'
Note: This is the error you get when using a reserved keyword like 'class'. The actual error for invalid variable names starting with numbers or containing hyphens may be similar.
Fix: Follow variable naming rules:
int numUsers{10}; // Start with letter
int userName{5}; // Use camelCase or underscores
int studentClass{8}; // Avoid reserved keywords
Data Type Errors
4. Type Mismatch in Assignment
Problematic code:
#include <iostream>
int main() {
int number = "Hello"; // String to int
char letter = 65.5; // Double to char
bool flag = "true"; // String to bool
return 0;
}
Error message:
error: invalid conversion from 'const char*' to 'int' [-fpermissive]
Note: This example shows the error for just the string-to-int conversion. Similar errors occur for the other type mismatches.
Fix: Match types correctly:
int number{42}; // int to int
char letter{'A'}; // char literal
bool flag{true}; // bool literal
5. Integer Overflow Warnings
Problematic code:
#include <iostream>
int main() {
short smallNumber = 50000; // Too big for short
char grade = 300; // Too big for char
return 0;
}
Error messages:
warning: implicit conversion from 'int' to 'short' changes value from 50000 to -15536
warning: implicit conversion changes signedness
Fix: Use appropriate data types:
int largeNumber{50000}; // Use int for larger values
char grade{'A'}; // Use char for characters, not large numbers
Expression and Operator Errors
6. Missing Operators
Problematic code:
#include <iostream>
int main() {
int a{5};
int b{3};
int result = a b; // Missing operator between variables
return 0;
}
Error message:
error: expected ';' before 'b'
Fix: Add the missing operator:
int result{a + b}; // Addition
int result{a * b}; // Multiplication
7. Invalid Operations on Different Types
This example uses
std::string
which we haven't covered yet (we will!). This is for demonstration purposes, you don't need to understand strings right now.
Problematic code:
#include <iostream>
int main() {
std::string name = "Alice";
int age = 25;
std::string result = name + age; // Can't add string + int directly
return 0;
}
Error message:
error: no operator "+" matches these operands
Fix: Convert types appropriately:
std::string result = name + std::to_string(age); // Convert int to string first
8. Division by Zero (Runtime Warning)
Problematic code:
#include <iostream>
int main() {
int a{10};
int b{0};
int result{a / b}; // Division by zero
std::cout << result << std::endl;
return 0;
}
Note: This compiles but causes undefined behavior at runtime.
Fix: Check for zero before dividing:
if (b != 0) {
int result{a / b};
std::cout << result << std::endl;
} else {
std::cout << "Error: Division by zero!" << std::endl;
}
Input/Output (iostream) Errors
9. Missing std:: Prefix
Problematic code:
#include <iostream>
int main() {
cout << "Hello World" << endl; // Missing std:: prefix
return 0;
}
Error message:
error: use of undeclared identifier 'cout'
error: use of undeclared identifier 'endl'
Fix: Add the std:: prefix:
std::cout << "Hello World" << std::endl;
10. Wrong Stream Direction
Problematic code:
#include <iostream>
int main() {
int age;
std::cout >> "Enter age: "; // Wrong direction for output
std::cin << age; // Wrong direction for input
return 0;
}
Error message:
error: no match for 'operator>>' (operand types are 'std::ostream' and 'const char [12]')
Note: The actual error message is quite long and technical, but the key part is "no match for 'operator>>'" which tells you you're using the wrong operator direction.
Fix: Use correct stream directions:
std::cout << "Enter age: "; // << for output
std::cin >> age; // >> for input
11. Mismatched Quotes
Problematic code:
#include <iostream>
int main() {
std::cout << "Hello World'; // Mixed quote types
std::cout << 'Hello World"; // Wrong quote for string
return 0;
}
Error messages:
error: missing terminating " character
warning: multi-character character constant
Fix: Use consistent quote types:
std::cout << "Hello World"; // Double quotes for strings
std::cout << 'A'; // Single quotes for single characters
Expression Statement Errors
12. Unused Expression Results
Problematic code:
#include <iostream>
int main() {
int a{5};
int b{3};
a + b; // Expression result is unused
return 0;
}
Warning message:
warning: expression result unused
Fix: Use the result or make the intention clear:
int result{a + b}; // Store the result
std::cout << a + b << std::endl; // Use the result directly
Initialization Errors
13. Uniform Initialization Issues
Problematic code:
#include <iostream>
int main() {
int x{3.14}; // Narrowing conversion not allowed with {}
return 0;
}
Error message:
error: type 'double' cannot be narrowed to 'int' in initializer list
Fix: Use compatible types or explicit conversion:
int x{3}; // Use integer literal
double x{3.14}; // Use double variable
int x{static_cast<int>(3.14)}; // Explicit conversion
Conditional Logic Errors
14. Assignment vs Comparison in if Statements
Problematic code:
#include <iostream>
int main() {
int age{18};
if (age = 21) { // Assignment instead of comparison
std::cout << "Can drink" << std::endl;
}
return 0;
}
Warning message:
warning: suggest parentheses around assignment used as truth value
Fix: Use comparison operator:
if (age == 21) { // Comparison
std::cout << "Can drink" << std::endl;
}
15. Missing Braces in if Statements
Problematic code:
#include <iostream>
int main() {
int score{85};
if (score > 90)
std::cout << "Grade: A" << std::endl;
std::cout << "Excellent work!" << std::endl; // Always executes!
return 0;
}
This compiles but doesn't work as expected.
Fix: Always use braces:
if (score > 90) {
std::cout << "Grade: A" << std::endl;
std::cout << "Excellent work!" << std::endl;
}
Practice Exercises
Further Error Practice
Practice debugging the exact error types covered in the C++ Error Reference lesson. Each error in this exercise corresponds to a specific example from the reference guide.
Lesson Discussion
Share your thoughts and questions