Jump to content
  • Advertisement
Sign in to follow this  

C++ strings -- WTF?

This topic is 4428 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

Ever since I switched from Code::Blocks (using mingw) to VS 2005, I have been having trouble getting strings working how I want. The first thing I noticed was that instead of being able to output a string directly using cout, I had to print out the c-string member of it: using std::string; using std::cout; using std::endl; string name; //cout << name << endl; <--- gives an error, never gave error w/ mingw cout << name.c_str() << endl; //okay with VS 2005 The second thing I noticed was I couldn't compare two strings with the == operator. I saw (via intellisense) that the string class had a compare function, so I tried using that, and I also tried comparing c-strings. string nameA = "ASDF"; string nameB = nameA; //nameA == nameB; <--- gives error nameA.compare(nameB); //returns false nameA.c_str() == nameB.c_str() //also returns false I don't really care about the output thing (I figure it's just VS 2005 being standards compliant), however I don't like not being able to compare strings. What can I do to comapare them?

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by PunaProgrammer chris
//nameA == nameB; <--- gives error


The line above will not actualy do anything, perhaps try:

if(nameA == nameB)
{

}

Quote:
Original post by PunaProgrammer chris
nameA.compare(nameB); //returns false


That should return a non-zero value.

Quote:
Original post by PunaProgrammer chris
nameA.c_str() == nameB.c_str() //also returns false


That is pretty much just nonsense to compare to pointers like that.

Share this post


Link to post
Share on other sites
Quote:
Original post by C J W
Quote:
Original post by PunaProgrammer chris
//nameA == nameB; <--- gives error


The line above will not actualy do anything, perhaps try:

if(nameA == nameB)
{

}


Well, that actually is the way I'm using it, but considering I'm getting a compiler error, it shouldn't matter.

Quote:
Original post by C J W

Quote:
Original post by PunaProgrammer chris
nameA.compare(nameB); //returns false


That should return a non-zero value.


But it isn't.

Quote:
Original post by C J W
Quote:
Original post by PunaProgrammer chris
nameA.c_str() == nameB.c_str() //also returns false


That is pretty much just nonsense to compare to pointers like that.


Yeah, but nothing else was working, so I decided to go out on a limb.

Share this post


Link to post
Share on other sites
I honestly can not understand what is going on then. Perhaps start a new C++ project and try again. Perhaps reinstall MSVC 2k5. Perhaps go back to mingw, because that behaviour sounds realy odd and goes against the standard completely.

Share this post


Link to post
Share on other sites

#include <iostream>
#include <string>

using std::string;
using std::cout;
using std::endl;
int main()
{

string name;
cout << name << endl;

}


This compiles for me using VS 2005. Maybe your standard headers got corrupted somehow?

Share this post


Link to post
Share on other sites
Oops.
My thought process looking at the last post:
"
Quote:
Original post by sjelkjd
#include <string>

Hmm...
Quote:
Original post by sjelkjd
#include <string>

Uhhh...
Quote:
Original post by sjelkjd
#include <string>

OH! Missing header!"

HAHAHAHAHA. That explains it.
Sorry for this dumb waste of time.

Share this post


Link to post
Share on other sites
I've had some problems to with strings, but my app was crashing when I tried to return one from a function.. I the function was in another dll from the caller, and I think something went wrong with the deallocation..

Are you supposed to be able to compare with ==? I couldn't, but the compare method worked fine as far as I could tell..

Share this post


Link to post
Share on other sites
Quote:
Original post by PunaProgrammer chris
Oops.
My thought process looking at the last post:
"
Quote:
Original post by sjelkjd
#include <string>

Hmm...
Quote:
Original post by sjelkjd
#include <string>

Uhhh...
Quote:
Original post by sjelkjd
#include <string>

OH! Missing header!"

HAHAHAHAHA. That explains it.
Sorry for this dumb waste of time.



Wait a sec.. But you could compare them? I mean, didn't it complain about that compare method?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!