中級 › C++でerror: assignment of read-only variable が出る原因と直し方 › パターン3
C++でerror: assignment of read-only variable が出る原因と直し方
constを付けて宣言した変数に、あとから値を代入しようとしたときに出るエラーです。
1 #include <iostream> 2 int main() { 3 const int taxRate = 10; 4 ___________ = 20; ^ 5 std::cout << taxRate << std::endl; 6 return 0; 7 }
main.cpp:4:13: error: assignment of read-only variable 'taxRate'
空欄を埋めてコンパイル・実行を通す
「constは目印にすぎない」というのは誤解で、実際にコンパイラがチェックする言語機能です。
次の問題