C++ const Error | 'passing as const' Compile Error Complete Solution
이 글의 핵심
C++ const error C++, const, "passing, Introduction: "Keep getting passing as const error" explained in detail with practical examples.
Introduction: “Keep Getting passing as const Error"
"Don’t Know Where to Put const”
In C++, const is a key keyword for improving type safety, but const-related errors are among the most common compile errors for beginners.
Below is an implementation example using C++. Ensure stability through error handling. Understand the role of each part while examining the code.
// ❌ Error code
void print(std::string& s) { // Non-const reference
std::cout << s << '\n';
}
int main() {
print("Hello"); // Temporary object → only const reference allowed
}
// error: cannot bind non-const lvalue reference of type 'std::string&'
// to an rvalue of type 'std::string'
What This Guide Covers:
- 10 const-related error patterns
- const reference vs non-const reference
- const member functions
- mutable keyword
- const_cast usage and precautions
1. Ten Common const Errors
Error 1: passing as const (Most Common)
Below is an implementation example using C++. Ensure stability through error handling. Understand the role of each part while examining the code.
// ❌ Error code
void modify(std::string& s) { // Non-const reference
s += " world";
}
int main() {
modify("Hello"); // Temporary object cannot bind to non-const reference
}
// error: cannot bind non-const lvalue reference of type 'std::string&'
// to an rvalue of type 'std::string'
Solution:
Below is an implementation example using C++. Try running the code directly to check its operation.
// ✅ Change to const reference
void print(const std::string& s) { // const reference
std::cout << s << '\n';
}
int main() {
print("Hello"); // OK
}
Error 2: discards qualifiers
Below is an implementation example using C++. Define a class to encapsulate data and functionality, ensure stability through error handling. Understand the role of each part while examining the code.
// ❌ Error code
class MyClass {
int value_;
public:
int getValue() { // No const
return value_;
}
};
void print(const MyClass& obj) {
std::cout << obj.getValue() << '\n'; // Calling non-const function on const object
}
// error: passing 'const MyClass' as 'this' argument discards qualifiers
Solution:
Below is an implementation example using C++. Define a class to encapsulate data and functionality. Try running the code directly to check its operation.
// ✅ const member function
class MyClass {
int value_;
public:
int getValue() const { // Add const
return value_;
}
};
Error 3: assignment of read-only variable
Below is an implementation example using C++. Ensure stability through error handling. Try running the code directly to check its operation.
// ❌ Error code
void foo() {
const int x = 42;
x = 99; // Modifying const variable
}
// error: assignment of read-only variable 'x'
Solution: Remove const or use new variable.
Below is an implementation example using C++. Try running the code directly to check its operation.
// ✅ Remove const
void foo() {
int x = 42;
x = 99; // OK
}
2. const Reference vs Non-const Reference
Comparison Table
| Type | Can Bind Temporary | Can Modify | Use Case |
|---|---|---|---|
T& | ❌ | ✅ | Modify parameter |
const T& | ✅ | ❌ | Read-only parameter |
T&& | ✅ (rvalue) | ✅ | Move semantics |
Example
void modify(std::string& s) { // Non-const reference
s += " world";
}
void print(const std::string& s) { // const reference
std::cout << s << '\n';
}
int main() {
std::string str = "Hello";
modify(str); // OK: lvalue
modify("Hello"); // ❌ Error: temporary
print(str); // OK: lvalue
print("Hello"); // ✅ OK: temporary can bind to const reference
}
Summary
Key Points
- const reference: Can bind temporaries, read-only
- const member function: Cannot modify member variables
- mutable: Allows modification in const functions
- const_cast: Avoid if possible
- const correctness: Improves type safety
When to Use
✅ Use const when:
- Read-only parameters
- Member functions not modifying state
- Preventing accidental modification
- Improving type safety
❌ Don’t use const when:
- Need to modify parameter
- Need to modify member variables
- Unnecessary complexity
Best Practices
- ✅ Use const reference for large objects
- ✅ Mark non-modifying functions as const
- ✅ Use mutable for logical const
- ❌ Don’t use const_cast unnecessarily
- ❌ Don’t ignore const correctness
Related Articles
- C++ Common Beginner Errors
- C++ Template Error Messages
- C++ Auto Type Deduction
Master const for safer C++ code! 🚀