Further adventures in C++

When I write code like

if (doSomething())
str = SOME_CONSTANT;
else
doSomethingElse();

I am usually pretty sure that this works. But if the compiler then tells me

error C2181: illegal else without matching if

I really start scratching my head. Especially if it is already late at night and I need some sleep. So I sat there, sleepy, scanning these 4 lines of code for some invalid character I've typed in by accident. Do you see it?
I didn't either. Everything looks correct, right?
I finally got my sleep when I realized some CENSORED CENSORED CENSOREDhead had written this to define 'SOME_CONSTANT':

#define SOME_CONSTANT "sdfs";

But I guess programming would be boring without gems like this.

ten comments, already:

This is why #define is evil and should not be used unless you have no other option. Static const variables or enums are a far better choice for constants.
steve () (link) - 27 02 09 - 14:08

Lol … but #define s are a fine thing … use it with care – like many of the “C” things I like (like the ”?” operator) – and you can be happy. Use it without the care they need and you’ll get error messages you won’t understand.
Brainsaw (link) - 27 02 09 - 15:51

One reason why I try to write the brackets even if there’s only one statement in the if/else block.

But yeah I know… we programmers are too lazy to type in this 4 more characters
Sylence () (link) - 27 02 09 - 16:01

SOME_CONSTANT is the first place I would have looked.
rbrt - 27 02 09 - 18:23

Well, use g++ next time:

bla.c:14: error: expected primary-expression before ‘else’
bla.c:14: error: expected `;’ before ‘else’
blah - 27 02 09 - 18:38

found this once: #define FOREVER for(::)
someone obviously tried to be funny.
grumble
KIENI - 28 02 09 - 06:05

@grumble: Its more like this:
#define EVER ;;
...
for (EVER)
{
DoThisAndThat();
}

P.S
I find it very funny :).
MasterGod () (link) - 28 02 09 - 19:11

Before else never have ”;”.
ngoac - 01 03 09 - 05:50

clearly the preprocessor is not mighty enough, because it can just insert the text and not process it. SOME_CONSTANT#SUBSTR or something like that would look proper ;-)
xaos - 02 03 09 - 12:39

This is why i hate the c++ programmers that come from c…
they use this CENSORED things…
why they don’t use the const?!?
is better, is usefull and is not as in c :)
revan1985 () (link) - 09 03 09 - 10:01


Name:  
Remember personal info?
yes
no
Email (optional):
URL (optional):
Enter "layered" (antispam):
Comment:Emoticons / Textile

  ( Register your username / Log in )

Notify: Yes, send me email when someone replies.  

Small print: All html tags except <b> and <i> will be removed from your comment. You can make links by just typing the url or mail-address.
Note: If you type in your email adress above, it will be visible to other visitors, although it will be hidden for bots using javaScript.