is private within this context C/C++ Origin: Application & API Last updated 15 Oct 2026

C++: error: 'int X::secret' is private within this context

Code outside the class tried to use a private (or protected) member.

Exact message you may see is private within this contextis protected within this context'int' is private within this contextis inaccessible within this contextis a private member of
Seen on: C/C++ GCC Clang

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 main or 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

  1. Add a public getter/setter or a public method that does the work
  2. Change to struct or add public: if the member should be public
  3. Declare friend only for tightly coupled helpers

Detailed fix by platform

C++

  1. Expose behaviour, not data: int secret() const { return secret_; }
  2. Use class D : public B when you want an is-a relationship

Code examples

Access control

cpp
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

  1. Access — Is the member private, protected or public?
  2. Inheritance — class or struct, public or private?
  3. 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:

Where this error sits

Origin: Application & API. Errors that are often confused with or caused by this one (click to open):

AddressSanitizer: heap-buffer-overflow — C/C++: ==ERROR: AddressSanitizer: heap-buffer-overflow on address — READ/WRITE of size N (C/C++)AddressSanitizer: hea…AddressSanitizer: heap-use-after-free — C/C++: ==ERROR: AddressSanitizer: heap-use-after-free on address — memory used after delete/free (C/C++)AddressSanitizer: hea…call of overloaded is ambiguous — C++: error: call of overloaded 'f(int)' is ambiguous (C/C++)call of overloaded is…cannot declare variable to be of abstract type — C++: error: cannot declare variable 'x' to be of abstract type 'Shape' (pure virtual functions) (C/C++)cannot declare variab…discards qualifiers — C++: error: passing 'const X' as 'this' argument discards qualifiers [-fpermissive] (C/C++)discards qualifiersincomplete type — C++: error: invalid use of incomplete type 'class X' — forward declaration only (C/C++)incomplete typeuse of deleted function — C++: error: use of deleted function 'X::X(const X&)' — copying a non-copyable type (C/C++)use of deleted functi…is privat…

Explore the map of this error Full error map →

🧠 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.