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