C++: error: 'int X::secret' is private within this context
Code outside the class tried to use a private (or protected) member.
is private within this contextis protected within this context'int' is private within this contextis inaccessible within this contextis a private member of Meaning
Members of a class are private by default; a struct defaults to public. Access is checked by the name's declaration, not by the object, so even a getter named differently is the fix. Friends and member functions of the same class are exempt.
Common causes
- Accessing a private data member from
mainor another class - Inheriting privately (the default for
class D : B) and converting to the base - Forgetting
public:after the first member of a class - Protected member accessed through a base-class object instead of
this
⚡ Quick fix
- Add a public getter/setter or a public method that does the work
- Change to
structor addpublic:if the member should be public - Declare
friendonly for tightly coupled helpers
Detailed fix by platform
C++
- Expose behaviour, not data:
int secret() const { return secret_; } - Use
class D : public Bwhen you want an is-a relationship
Code examples
Access control
class Account {
int balance_ = 0; // private by default
public:
int balance() const { return balance_; }
void deposit(int n) { balance_ += n; }
};
int main() {
Account a;
// a.balance_ = 5; // error: 'int Account::balance_' is private within this context
a.deposit(5);
return a.balance();
}How to diagnose
- Access — Is the member private, protected or public?
- Inheritance — class or struct, public or private?
- Caller — Is the code inside the class or a friend?
🔧 Still not fixed?
Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:
Similar errors
- AddressSanitizer: heap-buffer-overflow C/C++: ==ERROR: AddressSanitizer: heap-buffer-overflow on address — READ/WRITE of size N
- AddressSanitizer: heap-use-after-free C/C++: ==ERROR: AddressSanitizer: heap-use-after-free on address — memory used after delete/free
- call of overloaded is ambiguous C++: error: call of overloaded 'f(int)' is ambiguous
Most viewed in C/C++
- terminate called after throwing an instance of C++: terminate called after throwing an instance of 'std::out_of_range' — what(): vector::_M_range_check
- double free or corruption C/C++: free(): double free detected / double free or corruption (out) — Aborted (core dumped)
- undefined reference to C/C++: undefined reference to `function' — collect2: error: ld returned 1 exit status
Other ways to find it
Where this error sits
Origin: Application & API. Errors that are often confused with or caused by this one (click to open):
🧠 Still stuck? Analyze your error
Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.
💬 What worked for others
No tips yet — did something else fix this for you?
Share what worked for you
Tips are checked by an editor before they appear. Links are removed.
Report a correction or suggest an improvement
Last updated 15 Oct 2026