How to assign a value to a variable inside of a class? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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

27th May 2021, 7:02 AM
Mani_K_A
2 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 :)
27th May 2021, 7:20 AM
Mihail
Mihail - avatar
0
I tried but it gives me this error : 'variable' in namespace '::' does not a name type
27th May 2021, 7:31 AM
Mani_K_A