string class

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Guest

string class

Post by Guest »

ok this question is not directly to do with irrlicht but i am just wondering why the string is not working with code::blocks
anyone else got this problem?
MiNalien
Posts: 9
Joined: Mon Feb 06, 2006 6:05 pm

Post by MiNalien »

What, exactly, are you having problems DOING? Are you getting errors? What files are you including, which compiler you're using (Code::Blocks can use a few different compilers), you have to give a lot more information before anybody can help you.
Guest

Post by Guest »

I had trouble with it, try this:

Code: Select all

#include <string>

using namespace std;

int main()
{
   std::string sString = "Hello";
   sString += " World";

   return 0;
}
Guest

Post by Guest »

Anonymous wrote:I had trouble with it, try this:
What trouble did that give you? A compiling error? A linking error? Id doesn't run? It crashes? Copypaste any errors you get, you haven't provided enough information for anyone to help you.
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

Well, I don't know what the problem really is, too...
But should the include not be like this:

Code: Select all

 #include <string.h>
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
jam
Posts: 409
Joined: Fri Nov 04, 2005 3:52 am

Post by jam »

Acki wrote:Well, I don't know what the problem really is, too...
But should the include not be like this:

Code: Select all

 #include <string.h>
<string>

and

<string.h>

Can both be used. However on most of the compilers I've used if you use the second one you get a warning about using antiquated headers.


For the OP the code you posted works just fine with my macine, what errors are you getting??
vitek
Bug Slayer
Posts: 3919
Joined: Mon Jan 16, 2006 10:52 am
Location: Corvallis, OR

Post by vitek »

Actually, the two are not interchangeable.

You must #include <string> if you want to use the C++ Standard Library string classes [std::basic_string, std::string and std::wstring]. If you #include <string.h> you will get the Standard C Library string functions [strcmp, strstr, strchr, ...].

If the following compiles, the C libraries are not compliant.

Code: Select all

#include <string.h>
std::string s;
The code posted originally is correct should most definitely compile. As we've all said, it would be nice to see an error message.

Travis
Guest

Post by Guest »

ok i have the #include <string> ( and using namespace std; using std::string) and if i try for example:
string s; or string s = "text"; i always get the `string' undeclared (first use of this function) error
Guest

Post by Guest »

ok i got it working with

Code: Select all

#include <string> 

using namespace std; 

int main() 
{ 
   std::string sString = "Hello"; 
   sString += " World"; 

   return 0; 
}
but just one last (stupid) question: is it possible to have an array of strings? how?

and thanks for all the help [/code]
Guest

Post by Guest »

ok nevermind figured it out :lol:
but the problem m having now is that i want a random string from the array (thats easy) but the FMOD function i'm passing that string into only takes char[]. is there a way to convert the string to a char[]?
Baal Cadar
Posts: 377
Joined: Fri Oct 28, 2005 10:28 am
Contact:

Post by Baal Cadar »

std::string::c_str() does this. See STL docs for details.
Guest

Post by Guest »

cheers it worked perfectly :)
Guest

Post by Guest »

Anonymous wrote:

Code: Select all

using namespace std; 
std::string sString = "Hello"; 
BTW, you don't need both "using namespace std;" and "std::string". They're interexchangable. But if you omit the "using.." be sure to put "std::" infront of every string or whatever it is that you're using from the std namespace.
jam
Posts: 409
Joined: Fri Nov 04, 2005 3:52 am

Post by jam »

vitek wrote:Actually, the two are not interchangeable.
Yep I was wrong, In Dev-C++ you can even open the header file up and it has a warning in the preamble saying extactly that.
Post Reply