+ 2
When iam trying to make a class with an attribute string, it work like this: string name = " "; not like that: string name; why?
#include <iostream> #include <string> using namespace std; class myClass { public: string name; //doesnt work for me like this. }; int main() { myClass myObj; myObj.name = "SoloLearn"; cout << myObj.name; return 0; }
38 Respuestas
+ 2
Abdalla Mohamed ooo, ok it is no problem. create constructor in class and initialize member variable.
public: MyClass::MyClass(){name =“”;}
+ 2
Abdalla Mohamed i was update my code with constructor. take it and check on your syatem
+ 1
... i try wait a minute!
+ 1
Works perfectly!!!!!
https://code.sololearn.com/cGLcuhgHbyKy/?ref=app
+ 1
Abdalla Mohamed are you check it?! Problem was in constructor?!
+ 1
It is strange((
+ 1
I'm guessing your getting a null reference error....
If so it is because you cannot use something that is nothing (i.e. null)
The compiler needs it to be something.
"" is giving it a default of empty string which is something.
+ 1
Yes i got it, thank you.
+ 1
I got the PROBLEM
YOU CAN just USE " char " in place of " string "!!!
+ 1
Yeah
0
?? Worked fine for me in code::blocks.
0
Iam using code blocks too and i know the code is working for you but it doesnt work for me.
So I've got this error.
Error: myClass::name should be initialized in the member initialization list [-werror=effc++]
0
yah thank you very much
0
Actually its not working too :(
0
I dont know whats going on
0
The same error
0
Yup i think the problem in my console.
But how , idont know
0
Thank you anyway for your time :)
0
Hello
0
Me too