+ 1

Am trying to create a string attribute in Codeblocks but am told "string does not define a type"

I had created separate header and source files with codeblocks, made a string attribute in header file as private member, tried running code en it told me... What could be the issue? Sorry I can't post the piece of code!

14th Nov 2019, 10:13 PM
Hilary
Hilary - avatar
8 Respostas
+ 2
Did you include string? Did you forget to use std:: ? It's just guess work without you posting the code, m8.
14th Nov 2019, 10:27 PM
Dennis
Dennis - avatar
+ 2
Okay. Thanks now I know. I'll be detailed nxt tym.
15th Nov 2019, 4:29 AM
Hilary
Hilary - avatar
+ 1
I included string but never...wait do I need std:: to define an attribute?
14th Nov 2019, 10:33 PM
Hilary
Hilary - avatar
+ 1
You don't need to use std:: to define an attribute, int doesn't need one after all. It's because string is defined in the std namespace, so you have to tell the compiler where to look for it. You probably used "using namespace std;" in your previous codes, which tells the compiler to look in std by default, so you never needed it. However, you REALLY shouldn't do that in a header file as you'll pollute the global namespace for literally everyone that uses it. You're not gonna make friends with that style. ( and you might as well use good coding practices, even when it's a private project ) You might have used it in your cpp file, but your header doesn't know about what goes on in there. ( codeblocks highlights most things defined in the std namespace as green, btw )
14th Nov 2019, 10:37 PM
Dennis
Dennis - avatar
+ 1
So no string attributes for now...but I guess if I don't separate the code (as in header en source) it's possible to use string to define attributes...isn't it?
14th Nov 2019, 11:02 PM
Hilary
Hilary - avatar
+ 1
All I'm saying is is that you should use std::string instead of just string if you want to use it.
14th Nov 2019, 11:05 PM
Dennis
Dennis - avatar
+ 1
Let me give a try...
14th Nov 2019, 11:07 PM
Hilary
Hilary - avatar
0
"Sorry I can't post the piece of code!" Does this mean you don't know how? or is it something else? Follow this guide for sharing links around SoloLearn forum, if it was for the former. Otherwise, it's rather difficult for any of us to assist you without having a look at your code. But it's your choice : ) https://www.sololearn.com/post/74857/?ref=app
15th Nov 2019, 4:16 AM
Ipang