我想创建一个Assert函数,它在不打开控制台的情况下在新窗口中显示消息。该函数需要独立于O.S,如果可能,不要使用c ++外部库。
#include <string>
#include <sstream>
#ifdef WIN32
#include <windows.h> // include windows header, for Windows Based Sistems.
#else
// ...
#endif
void Assert (bool cond,const char* file,int line,const char* desc)
{
if (cond) return; // No Assertion.
#ifdef WIN32
// Use MessageBox function to display the information.
// For Example ...
std::stringstream st;
st << "There Was An Error At Runtime ! \n";
st << "File: " << file << "\n";
st << "Line: " << line << "\n";
st << "Description: " << desc << "\n";
st << "Do You Want To Continue Running the Application?\n";
if (MessageBox (NULL,"Unexpected Error", str.str ().c_str (), MB_YESNO) == IDNO)
exit (-1);
#else
// Do Something, but in Unix Base Systems.
#endif
}
#define assert(condition,description) \
__assert__ (condition,__FILE__,__LINE__,description)
需要C ++代码在其他O.S中输出MessageBox
答案 0 :(得分:1)
C ++标准不包括GUI操作。 将需要使用外部库,为所需的平台提供GUI服务。
所以你要求的是不可能的。遗憾。