/* SpriteError - used to display errors when sprites can't be loaded */
void SpriteManager::SpriteError(char FileName[])
{
char ErrorString[]="Error loading file "<<FileName<<". TicTacToe will now exit.";
MessageBox(NULL,ErrorString,"Error loading file",MB_OK|MB_ICONSTOP);
}
/* End SpriteError */
Passing items into MessageBox()
I'm working on my own SDL wrapper system to slash my development time on future projects. I've hit a minor snag, however.
Although this is an ordinary SDL application, I'm using the Win32 MessageBox() function to generate Message Boxes for errors. Here's an error-handler function for when the program tries to load a sprite, and can't find it.
What I want is for the variable FileName to be passed into ErrorString[] so that it appears in the Message Box. Let's say the program tried to load "data/dog.bmp", and it didn't exist, so "data/dog.bmp" is passed as a parameter to the error handler. The Message Box should say "Error loading file data/dog.bmp. TicTacToe will now close" (I will be changing the program name manually for each project.
The problem is, the way I have it, it won't compile, stating:
37 C:\<removed for clarity>\spritewrapper.cpp invalid operands of types `const char[20]' and `char*' to binary `operator<<'
The error is occurring in the ErrorString[] line, but I'm not sure how to get round the problem. MessageBox wouldn't accept std::string when I tried that.
Can you help?
ukdeveloper.
ukdeveloper,
Rather than passing in a character array, pass in a character pointer, ie (char*). So your function would look like:
This should work fine.
Rather than passing in a character array, pass in a character pointer, ie (char*). So your function would look like:
/* SpriteError - used to display errors when sprites can't be loaded */void SpriteManager::SpriteError( char* pFileName ){ char ErrorString[]="Error loading file "<< pFileName <<". TicTacToe will now exit."; MessageBox(NULL,ErrorString,"Error loading file",MB_OK|MB_ICONSTOP);}/* End SpriteError */
This should work fine.
Thanks for your help, I've made the changes you suggested, but it's still generating the same error.
Oh crap,
I didn't look over the function thoroughly enough.
Within the function you're trying to assign a stream to a character array. The language doesnt allow you to do that. In order to accomplish the concatenation of the stream you must use standard string manipulation functions such as strcat, etc...Or, if you're fine with dynamic allocation you can use sprintf. Here's a working example:
I didn't look over the function thoroughly enough.
Within the function you're trying to assign a stream to a character array. The language doesnt allow you to do that. In order to accomplish the concatenation of the stream you must use standard string manipulation functions such as strcat, etc...Or, if you're fine with dynamic allocation you can use sprintf. Here's a working example:
void SpriteError( char* pFileName ){ char* preMessage = "Error loading file "; char* postMessage = ". TicTacToe will now exit."; // Dynamically allocate the memory to store the buffer char* pBuffer = new char[ strlen( preMessage ) + strlen( postMessage ) + strlen( pFileName ) + 1]; // Combine the string and store it in pBuffer sprintf( pBuffer, "%s%s%s", preMessage, pFileName, postMessage ); // Output the message and then clean up the allocated memory MessageBox( NULL, pBuffer, "Error loading file", MB_OK|MB_ICONSTOP ); delete [] pBuffer;}
In C++ char * is spelt std::string.
Enigma
/* SpriteError - used to display errors when sprites can't be loaded */void SpriteManager::SpriteError(std::string const & fileName){ std::string errorString = "Error loading file " + fileName + ". TicTacToe will now exit"; MessageBox(NULL, errorString.c_str(), "Error loading file", MB_OK | MB_ICONSTOP);}
Enigma
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement