中級 › error: is private within this context の原因と直し方 › パターン1
error: is private within this context の原因と直し方
private指定されたクラスのメンバ変数に、クラスの外から直接アクセスしようとしたときに出るエラーです。
1 #include <iostream> 2 class Account { 3 _________: ^ 4 int balance = 0; 5 }; 6 int main() { 7 Account a; 8 a.balance = 100; 9 std::cout << "done" << std::endl; 10 return 0; 11 }
main.cpp:8:7: error: 'int Account::balance' is private within this context
空欄を埋めてコンパイル・実行を通す
balanceはprivateなので、クラスの外にあるmain関数から直接触ることはできません。外部から操作させたいならpublicにする必要があります。
次の問題