Code Fix

中級error: is private within this context の原因と直し方 › パターン3

error: is private within this context の原因と直し方

private指定されたクラスのメンバ変数に、クラスの外から直接アクセスしようとしたときに出るエラーです。

 1  #include <iostream>
 2  class Wallet {
 3  _________:
        ^
 4      int coins = 0;
 5  };
 6  int main() {
 7      Wallet w;
 8      w.coins = 20;
 9      std::cout << "done" << std::endl;
10      return 0;
11  }
main.cpp:8:7: error: 'int Wallet::coins' is private within this context

空欄を埋めてコンパイル・実行を通す

広告
広告スロット(未設定)

error: is private within this context の原因と直し方

カプセル化のためにprivateメンバは外部から直接触れないようになっています。

このエラーの解説と類題をまとめて見る →

広告
広告スロット(未設定)