void CTiles::SetTile(string sNomtile)
{
strTile.tile = SDL_LoadBMP(sNomtile);
}
Error with LoadBMP
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
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?
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());}
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;
instead of:
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:
string Add(string X,const string& Y)
{
X += Y;
return X;
}
const std::string& sNomtile;
instead of:
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:
string Add(string X,const string& Y)
{
X += Y;
return X;
}
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:
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());}
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement