中級 › C++でerror: assignment of read-only variable が出る原因と直し方 › パターン4
C++でerror: assignment of read-only variable が出る原因と直し方
constを付けて宣言した変数に、あとから値を代入しようとしたときに出るエラーです。
1 #include <iostream> 2 int main() { 3 const int cap = 50; 4 ________ = 100; ^ 5 std::cout << cap << std::endl; 6 return 0; 7 }
main.cpp:4:9: error: assignment of read-only variable 'cap'
空欄を埋めてコンパイル・実行を通す
constの変数への代入はコンパイラが必ず拒否します。値を変えたい場合は別の変数を用意します。
次の問題