0
How to assign a value to a variable inside of a class?
I declared a global variable and I tried to change the value of it but it gives me : error 'variable' does not name a type ``` #include <iostream> int variable = 2343; class Something { variable = 33223; }; int main() { // nothing } ``` Is there a way to assign a value to it? Also, I won't declare the variable inside of class
4 Answers
0
Yes,you need to apply the scope operator :: . So the code would go
::variable=33223;
The way you tried to change the variable, is making the compiler think you are trying to create a new variable for the class.
But I don't think you can do it in a class like you have done.
Maybe put it in a function.
Hope it helps :)
0
I tried but it gives me this error
: 'variable' in namespace '::' does not a name type