我是编程新手,我在翻阅一本书时发现了这段代码。我相信它给出了一个如何使用已定义的 assert() 宏的示例。它不能在 code::blocks 10.05 上编译。我收到诸如
之类的错误- '#'后面没有跟宏参数
- 未终止#else
- 函数“int main()”中的“ASSERT”未在此范围内声明
代码:
#include<iostream>
#define DEBUG
#ifndef DEBUG
#define ASSERT(x)
#else
#define ASSERT(x)\
if(!(x))\
{\
cout<<"Error!!Assert"<<#x<<"failed\n";\
cout<<"on line"<<__LINE__<<"\n";\
cout<<"in file"<<__FILE__<<"\n";\
}\
#endif
using namespace std;
int main()
{
int x = 5;
cout<<"\nFirst assert.";
ASSERT(x==5);
cout<<"\nSecond assert.";
ASSERT(x!=5);
cout<<"\nDone."<<endl;
return 0;
}
如有任何帮助,我们将不胜感激。提前致谢。
最佳答案
if(!(x))\
{\
cout<<"Error!!Assert"<<#x<<"failed\n";\
cout<<"on line"<<__LINE__<<"\n";\
cout<<"in file"<<__FILE__<<"\n";\
} // no backslash
关于c++ - 使用一个简单的 assert() 宏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4039025/