The joys of C++

October 19th, 2007

Quick, why does this give me a compiler error?


  if      (dIntersectionZ <  box.GetChild(0,0,0).GetBox().Min.z) Assert(false);
  else if (dIntersectionZ <  box.GetChild(0,0,1).GetBox().Min.z) z = 0;
  else if (dIntersectionZ <  box.GetChild(0,0,2).GetBox().Min.z) z = 1;
  else if (dIntersectionZ <= box.GetChild(0,0,2).GetBox().Max.z) z = 2;
  else                                                           Assert(false);
 

The compiler complains that the else in the second line does not belong to any if.

Assert isn’t a function, duh!

From MyAssert.h, included in some completely inobvious place:


    #define Assert(x)   if (!(x)) {printf("Error:\n  File %s\n  Function %s\n  →
    Line %i;,__FILE__,__FUNCTION__,__LINE__);{ static FILE *fOut; static char  →
    sName[100]; _snprintf_s(sName,99,"Error_%x.log",sName); fopen_s(&fOut,sNam →
    e,"wb"); if(fOut){fprintf(fOut,"Error:\n  File %s\n  Function %s\n  Line % →
    i",__FILE__,__FUNCTION__,__LINE__);fclose(fOut);}} assert(0); exit(999);}
 

Moral of the story:
Preprocessor macros make for some really entertaining afternoons.
Oh, and avoid C++ wherever possible.

2 Responses to “The joys of C++”

  1. felix Says:

    uuuuh. painful

  2. nicolas Says:

    “Assert()” with upper case “A” defined in “MyAssert.h” seems to be some self-made assert.

    The STL-assert “assert()” is defined in “assert.h”.
    That’s also just a macro but hopefuly some better made one..

    Curly brackets around the body should do the job:

    define Assert(x) { if(!x) ... }

Leave a comment