• ### Popular Now

• 16
• 15
• 12
• 9
• 10

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

## Recommended Posts

I have a structure dans hold the info i need about a tile, it has a SDL_Surface where i can store my tile. I'v made a simple function that read a files and Initialise the map with the files information. But I have a problem, in the file i read the line, the row, then the path+files. 0 0 Bleu.bmp But when i do
void CTiles::SetTile(string sNomtile)
{
}


I get the folowing error : c:\Documents and Settings\AUBINM\Bureau\RPG1.1\Tiles.cpp(13): error C2664: 'SDL_RWFromFile' : impossible de convertir le paramètre 1 de 'std::string' en 'const char *' which mean in english something like, Cannot convert parameter 1 from std::string to const char * . What's wrong with my code?

##### Share on other sites
If you want to convert a string to a const char *, you should use .c_str() function. While string is a class, a char* is just an array of chars (terminated by a '\0').

void CTiles::SetTile(string sNomtile){  strTile.tile = SDL_LoadBMP(sNomtile.c_str());}

##### Share on other sites
Do what Lutyo said, but remember that often if you are not going to modify a string it will be far better to pass the string like this:
const std::string& sNomtile;
std::string sNomtile;

The difference:
The first is passed by reference, which means the parameter probably only will be 32 bits, while the string in most cases will be much bigger, so you gain a lot of speed.

There is two cases you dont want to pass by reference:
1: When you need to use the string as a temporary, maybe like this:
{
X += Y;
return X;
}

##### Share on other sites
Edit: Too slow. Maybe I should just quit posting...

Also, when passing strings (or any objects) as function parameters, you should use const-references if possible.

Like this:
void CTiles::SetTile(string const & sNomtile){  strTile.tile = SDL_LoadBMP(sNomtile.c_str());}

##### Share on other sites
Yeah thank guys.

And thank for the info on const reference.

I knew the cons of & but didn't know that for the string 32bits