使用简单的assert()宏
我是编程新手我在翻阅一本书时发现了这段代码。我相信它给出了如何使用定义的assert()宏的示例。它无法在 code::blocks 10.05 上编译。我收到错误,例如
- “#”后面没有宏参数
- 未终止#else
- in function 'int main()' 'ASSERT' was not statements in thisscope
代码:
#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;
}
任何帮助将不胜感激。提前致谢。
I'm new to programming & i found this code when i was going through a book. I believe it gives an example of how to use a defined assert() macro. It doesn't compile on code::blocks 10.05. I get errors such as
- '#' is not followed by a macro parameter
- unterminated #else
- in function 'int main()' 'ASSERT' was not declared in this scope
Code:
#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;
}
Any help would be greatly appreciated. Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)