I have some C++ code that returns this error:
error: assignment of read-only variable ‘parking’
The code:
char const * const parking= "false";
if (phidgets.value(PHIDGET3V_1) > 1000) {
parking = "true";
}
else{
parking = "false";
}
What does this error mean and how do I fix it?
You declared parking as constant pointer.
char const * const parking= "false";
So it will point only to string literal "false"
and may not be changed.
Also this statement
char const * const message = "Value: "+ parking +" ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxy";
is invalid. There is no addition operator for pointers.
parking
is set to be const
(char const * const parking = "false"
) so it cannot be modified.
When you do parking = "true"
it raises compile time error.
How to reproduce the problem very simply to illustrate:
#include <iostream>
int main(){
const int j = 5;
j = 7;
}
const
means constant, meaning you are not allowed to change it:
error: assignment of read-only variable ‘j’
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With