Jump to content
  • Advertisement

Archived

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

Strife

STL basic_string (and getline)

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!