#### Archived

This topic is now archived and is closed to further replies.

# STL basic_string (and getline)

This topic is 6506 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I''ve been playing around with STL lately, and yesterday I was trying to get the strings to work. But when I tried to declare a string like this... string s; Or something like it, I got an error that said something like string is an undefined identifier. I #include''d , so why am I getting this? Also, I got the same error when trying to use the getline() function. I assume that''s because I don''t know what header file that''s in . Which one is it in? Excuse me whilst I conquer Earth... Commander M (a.k.a. Crazy Yank) http://commanderm.8m.com CmndrM@gdnmail.net

##### Share on other sites
string is not a class built into the language (unlike java, or
vb iirc) ..
you have to include a library
also, if you didn''t know all of the C++ standard library
is inside it''s own namespace (to avoid potential name
collisions)
so, here''s how to use the string class

#include
#include
using std::string;

int main()
{
string s;
std::cin.getline(s , 10); //iirc, this should work.
// ^^^^^ qualification of namespace

return 0;

}
note here, i didn''t incorporate the whole std:: namespace into
the global namespace, as it''s not really needed..
however you could if you wanted by saying-->

using namespace std;

##### Share on other sites
Well, you''ll notice I said I #include''d (though I forgot to say ). So yes, I did include it. I understand the namespace crap, but I thought that was only if I am using rather than . Oh well, I''ll give that a try.

Excuse me whilst I conquer Earth...

Commander M
(a.k.a. Crazy Yank)
http://commanderm.8m.com
CmndrM@gdnmail.net

##### Share on other sites
Yes, it''s true you only need std::cout if you''re using <iostream.h> instead of <iostream>. Since you''re using <string>, though, you need to use std::string.

Also, if I were you, I''d always use the files without the .h. I''ve had collisions in the past, and it ain''t pretty.

##### Share on other sites
the .h files are only included for backward compatibility anyway. dont use em if you dont have to

1. 1
2. 2
3. 3
Rutin
18
4. 4
5. 5
JoeJ
12

• 14
• 22
• 9
• 31
• 18
• ### Forum Statistics

• Total Topics
632618
• Total Posts
3007478
• ### Who's Online (See full list)

There are no registered users currently online

×