compiler mystery...weird

I was trying to compile a very basic test program that was less than 170 lines of code. I kept getting "Expected unqualified id before "if"" errors. I went through my code multiple times and retyped a good portion of it, but to no avail. I took out my comments from the problem function...and it compiled and worked fine! I wrote a comment back in, and it game me errors! The comments were on their own lines, separate from code lines. Also, during my testing, I commented out a simple line of code (x+=5;), but it still tried to compile it. It gave an error on a line that was all comment, no code. I have never seen this before. Anyone else run into this or have any ideas as to why this happened?
Compilers don't spit errors for no reason. You had something wrong with your code.

Can you post a small program that reproduces the error message?
Last edited on
In my experience this kind of thing happened when I tried to recompile my code without saving the files, are you saving your files before compiling?
My IDE automatically saves the files before compilation. I'm not sure if this will reproduce the
message, but my code looked something like this:
1
2
3
4
5
if (dir==1){
x+=4;
y+=4;}
//lots of these with the occasional "//****************************\\" to help with readability
//when I took out the dividing comments, it compiled fine 
A backslash followed by a newline causes the compiler to ignore the newline.
So
1
2
// comment \\
hello
Is the same as
// comment \hello
\(0.o)/ __ \(o.0)/

I didn't know escape sequences were valid in comments, didn't even know there was a newline escape sequence....
learn something new every day...

That is actually done even before preprocessing.
That's why you can do this:
1
2
#define foo bar ( x, \
 y, z )  
And this:
1
2
string S = "foo\
bar";
I prefer:
1
2
string s="foo"
"bar";

This uses the char constant concatenation step.
Hmm, didn't know that forward slashes did that. That explains everything! lol
Topic archived. No new replies allowed.