Program unable to compile and run. Please help :-)

Started by
4 comments, last by KF 14 years, 9 months ago
Hi, I am writing and learning a program on debugging assertions. below is the source code : // A program to define a DEBUG macro with an "on" value of one - to control an ASSERT function. // ASSERT macro function is to eveluate a specified condition for a boolean value. #define DEBUG 1 #if( DEBUG == 1 ) #define ASSERT( expr ) cout << #expr << "..." << num ; #if( expr != true ) { cout << " Fails in file: " << __FILE__ ; cout << " at line: " << __LINE__ << endl ; } ; #else cout << " Succeeds" << endl ; #elif( DEBUG == 0 ) #define ASSERT( result ) cout << "Number is " << num << endl ; #endif int main() { int num = 9 ; ASSERT( num < 10 ) ; num++ ; ASSERT( num < 10 ) ; system ( "Pause" ) ; return 0 ; } The output result is shown below : 1>------ Build started: Project: Debugging assertions, Configuration: Debug Win32 ------ 1>Compiling... 1>Debugging assertions.cpp 1>d:\kean fatt document 1\c-plus-plus assignment\chp9 processing macros w vis c++\debugging assertions\debugging assertions\debugging assertions\debugging assertions.cpp(9) : fatal error C1017: invalid integer constant expression 1>Build log was saved at "file://d:\Kean Fatt Document 1\C-plus-plus assignment\Chp9 Processing macros w Vis C++\Debugging assertions\Debugging assertions\Debugging assertions\Debug\BuildLog.htm" 1>Debugging assertions - 1 error(s), 0 warning(s) ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== I have check the C1017, but still unable to solve the problem. Please help. Thank you very much. Your effort is greatly appreciated. Best Regards, KF ( Kean Fatt )
Advertisement
You need to re-read how to define macros. You've got lots of problems. One of which is that you're defining multi-line macros wrong.

Why does this need to be a macro and not just have a (simple) macro call a function?
Thank you RDragon1. I use macros because it is in the chapter of learning about precessing macro. Do you mean multiple directives wrong ? Actually what do you mean by multi macro ? I think I do not use multiple directives in one line. All directives is separated in different line. Would you please correct my program because I really cannot find out the mistakes. Thank you RDragon1. You have been very helpful.

Thank you RDragon1. Your effort is very much appreciated.

Best Regards,
KF ( Kean Fatt )
'num' is not defined here
#define DEBUG 1#if DEBUG == 1#define ASSERT(expression, num) std::cout << expression << "..." << num << ' ';if (!expression){    std::cout << "Fails in file: " << __FILE__;    std::cout << "at line: " << __LINE__ << '\n';}else{    std::cout << "Succeeds" << '\n';}#else#define ASSERT(expression, num) std::cout << "Number is " << num << '\n';#endifint main(){    int num = 9;    ASSERT((num < 10), num);    return 0;}



I'd prefer this though:
#define DEBUG 1void debug_assert(bool expression, int num){    #if DEBUG == 1    std::cout << expression << "..." << num << ' ';    if (!expression)    {        std::cout << "Fails in file: " << __FILE__;        std::cout << "at line: " << __LINE__ << '\n';    }    else    {        std::cout << "Succeeds" << '\n';    }    #else    std::cout << "Number is " << num << '\n';    #endif}int main(){    int num = 9;    debug_assert((num < 10), num);    return 0;}


Right, there is a bug in this syntax highlighter. The macro version, when doing multiple lines, should contain a '\' at the end of the line, if you also want the next line to be used for the macro definition.

Good luck.
[size="2"]SignatureShuffle: [size="2"]Random signature images on fora
There are two (syntatical) problems with you code, the first is that you are using #if and #else where you should just be using if and else. The second is, as RDragon1 pointed out, that you have a multi-line macro that is incorrect. With the code at the moment the macro ASSERT contains only cout << #expr << "..." << num;, you need to tell the pre-processor to include the following lines too, you do this by putting a \ at the end of each line, and it will be seen by the processor as if it is part of the previous line (note I have had to put a space after the \ in the examples to get the html formatting correct - you will have to remove these spaces) :

#if( DEBUG == 1 )// #if and #else are preprocessor directives for // conditionally including code for compilation - // you want the regular if and else here, i.e. not//#if( expr != true )//  ...//#else#define ASSERT( expr ) cout << #expr << "..." << num;\     if( expr != true )\     {\         cout << " Fails in file: " << __FILE__;\         cout << " at line: " << __LINE__ << endl;\     }\     else\     {\         cout << " Succeeds" << endl;\     }#elif( DEBUG == 0 )    #define ASSERT( result ) cout << "Number is " << num << endl ;#endif


There are several non-syntatical suggestions I could make, I'm assuming this is just a learning exercise so may not be that relevant. Firstly this is probably better implemented as function, you can use a macro to stringise the expression and pass the __FILE__ and __LINE__ to the function.

Secondly you probably don't want to spam standard output when an expression passes (this should hopefully happen a lot - which means you're going to get an std out full of "num < 10...10Succeeeds" type messages, which maybe useful for testing that your macro works, but it's probably going to get pretty annoying.

The fact that #define ASSERT( expr ) cout << #expr << "..." << num; is purely because you are using the macro where there is a variable called num, try this in a different function (or rename your variable) and you'll see that it's a bad idea. In any case for an assertion macro you don't really want to always display information, only upon failure should you display information.

Last of all I would suggest that you should just disable the ASSERT macro in non-debug builds, again you are just going to spam std out with useless information, which will get tedious and frustrating, especially if you actually want to output useful information.

Something along the lines of :

void assert(bool expression, const char *expressionString, const char * file, int line){    if( expression != true )    {        cout << expressionString;        cout << " fails in file: " << __FILE__;        cout << " at line: " << __LINE__ << endl;    }}#ifdef DEBUG#define ASSERT(expression) assert(expression, #expression, __FILE__, __LINE__)#else#define ASSERT(expression)#endif

Hi Decrius and _moagstar_ ,

Thank you for your reply. I am still trying it out and study on the materials. Anyway, you guys' advice have been a great help to me. Thank you Decrius and moagstar. Your effor is greatly appreciated.

Best Regards,
KF


This topic is closed to new replies.

Advertisement