class DXError
{
public:
DXError(const std::string& errorMessage, const std::string& fileName,
const std::string& function, int line)
: mErrorMessage(errorMessage), mFileName(fileName), mFunction(function),
mLine(line) {}
const char* ToString() const
{
std::string error;
error += "In file: " + mFileName + "\r\n";
error += "In function: " + mFunction + "\r\n";
error += "On line: " + boost::lexical_cast<std::string>(mLine) + "\r\n";
error += "\r\n" + mErrorMessage;
std::ofstream file("output.txt");
file << error;
return error.c_str();
}
private:
std::string mErrorMessage;
std::string mFileName;
std::string mFunction;
int mLine;
};
#define DX_ERROR(errorMessage) DXError(errorMessage, __FILE__, __FUNCTION__, __LINE__)
// later, catching the exception
catch(const DXError& e)
{
MessageBox(0, e.ToString(), "Error", 0);
return 0;
}
[C++/Win32 API] Problem with MessageBox
I made a small class for exceptions in my program, but when I try to show the error with a MessageBox, the text just consists of like 50 Ý's. The text is just printed to the file so I could see if the string was right and it was.Btw, if I have overloaded a function, __FUNCTION__ just give the name so I don't know which of the functions who threw the exception. Ofcourse I could check the line number, but is the a way to get more that the name with some other macro?
[Edited by - Eriond on November 29, 2004 12:32:58 PM]
You're returning a pointer to a local variable.
Solution? Just return a std::string and call c_str() when you call MessageBox, i.e.:
Enigma
class DXError{public: DXError(const std::string& errorMessage, const std::string& fileName, const std::string& function, int line) : mErrorMessage(errorMessage), mFileName(fileName), mFunction(function), mLine(line) {} const char* ToString() const { // error is a local variable std::string error; error += "In file: " + mFileName + "\r\n"; error += "In function: " + mFunction + "\r\n"; error += "On line: " + boost::lexical_cast<std::string>(mLine) + "\r\n"; error += "\r\n" + mErrorMessage; std::ofstream file("output.txt"); file << error; // return a pointer to the character data held by error // the return causes the destruction of error, so the pointer returned is no longer valid return error.c_str(); }private: std::string mErrorMessage; std::string mFileName; std::string mFunction; int mLine;};#define DX_ERROR(errorMessage) DXError(errorMessage, __FILE__, __FUNCTION__, __LINE__)// later, catching the exceptioncatch(const DXError& e){ MessageBox(0, e.ToString(), "Error", 0); return 0;}
Solution? Just return a std::string and call c_str() when you call MessageBox, i.e.:
MessageBox(0, e.ToString().c_str(), "Error", 0);
Enigma
In response to your second question, you can use the
__FUNCSIG__ macro to get the signature of the function.
__FUNCSIG__ macro to get the signature of the function.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement