C++ const Error — Complete Guide
이 글의 핵심
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.
// ❌ 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)
// ❌ 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:
// ✅ 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
// ❌ 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:
// ✅ const member function
class MyClass {
int value_;
public:
int getValue() const { // Add const
return value_;
}
};
Error 3: assignment of read-only variable
// ❌ 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.
// ✅ 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
Master const for safer C++ code! 🚀
Frequently Asked Questions (FAQ)
Q. When would I use this in practice?
A. Everything about C++ const Error : from basic concepts to practical applications.
Q. What should I read before this?
A. Follow the previous article or related articles links at the bottom of each post to learn in sequence. See the C++ series index for the full picture.
Q. Where can I study this more deeply?
A. Check cppreference and the relevant library’s official documentation. The reference links at the end of the article are also worth using.
Related Articles (Internal Links)
Other articles related to this topic.
- C++ 초보자가 자주 하는 실수 Top 15 | 컴파일 에러부터 런타임 크래시까지
- C++ 템플릿 에러 메시지 해석 | ‘수백 줄 에러’ 5분 만에 읽는 법
- C++ auto 타입 추론 에러 | ‘cannot deduce’ 컴파일 에러 해결
Keywords Covered in This Article (Related Search Terms)
This article covers C++, const, compile-error, const-correctness, type-safety, error-resolution.