开发者

using a simple assert() macro

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

  1. '#' is not followed by a macro parameter
  2. unterminated #else
  3. 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.


if(!(x))\
{\
       cout<<"Error!!Assert"<<#x<<"failed\n";\
       cout<<"on line"<<__LINE__<<"\n";\
       cout<<"in file"<<__FILE__<<"\n";\
} // no backslash
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜