Recommended Posts

I have started work on a project of my own trying to make an easily moddable RPG, I decided to use text documents to store the information for rooms, items, and image locations, but I have run into a snag while trying to get the string read from the file to be accepted by the SDL_LoadBMP function.
the error report is this:
/media/disk/Programming/C++ Projects/AdventureGame/main.cpp|76|error: cannot convert 'std::basic_string<char, std::char_traits<char>, std::allocator<char> >' to 'const char*' for argument '1' to 'SDL_RWops* SDL_RWFromFile(const char*, const char*)'|
///////////////////////////////////////////////////////////////////////////////
I am trying to load the information from the file like this:
// uses the iStream1 file stream to open the main text file
iStream1.open("main.txt", ios::in);
// Sends the file stream into the text string and loops through until an EOF
while(getline(iStream1,text))
{
// tracks the total amount of lines
line++;
//Outputs the unmodified text
//cout << text << endl;
//Finds the number of the current image
loc = text.find(":");
// Finds the number in the string
subtext = (text.substr(0,loc));
// Converts the string number into an integer
i = boost::lexical_cast<int>(subtext);
//Stores to the map
Images = text.substr(loc+1,text.size());
// Outputs the number derived with boost
//cout << i << endl;
}
//////////////////////////////////////////////////////////////////////////////
line is an int, loc is an int, subtext is a string, i is an int, Images is a map<int,string>
As far as I understand what this should do is: retrieve one line from iStream1 and store it into text, increment line, find te location of a colon and store that location in loc, get the sub string in between the beginning of the stream and loc since that should be a number, convert the sub string into a number using the boost library, create a new entry in the map using i as the key and the substring found after the colon as the value.
And it does seem to do that, what is wrong with the SDL bmp function that wont allow a string?

0

Share this post

Link to post

Share on other sites

You're sending it a C++ string, AKA std::string. SDL is a C API, and hence uses regular C-strings (which are just arrays of char's). This is why the documentation specifies that the parameters is a pointer to a char, and why the compiler complains that it can't convert from type "std::string" to "const char*". However the string class provides a function that returns a pointer to a C-string, called "c_str()". Just call that and send the result to SDL_LoadBMP.