Passing items into MessageBox()

Started by
6 comments, last by Enigma 18 years, 10 months ago
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.

/* 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 */

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.
Advertisement
ukdeveloper,

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.
Jeromy Walsh
Sr. Tools & Engine Programmer | Software Engineer
Microsoft Windows Phone Team
Chronicles of Elyria (An In-development MMORPG)
GameDevelopedia.com - Blog & Tutorials
GDNet Mentoring: XNA Workshop | C# Workshop | C++ Workshop
"The question is not how far, the question is do you possess the constitution, the depth of faith, to go as far as is needed?" - Il Duche, Boondock Saints
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:

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;}
Jeromy Walsh
Sr. Tools & Engine Programmer | Software Engineer
Microsoft Windows Phone Team
Chronicles of Elyria (An In-development MMORPG)
GameDevelopedia.com - Blog & Tutorials
GDNet Mentoring: XNA Workshop | C# Workshop | C++ Workshop
"The question is not how far, the question is do you possess the constitution, the depth of faith, to go as far as is needed?" - Il Duche, Boondock Saints
That works. Thanks!

(Rating++;)
std::string and c_str() might solve the problem
You can't use << with chars, right? It only works for streams if I am not mistaken.
In C++ char * is spelt std::string.

/* 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