Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bug through a single-line comment before a line with a statement [duplicate]

Tags:

c++

comments

Possible Duplicate:
A function-definition is not allowed here before ‘{’

I would like to share this bug I did in my code, one of the hard to find ones:

#include <iostream>

void StartNuclearWar() {
    std::cout << "War in progress..." << std::endl;
}

int main()
{
    int a = 0;

    // Get activation codes from C:\codes\
    a = 1;

    if(a == 0) {
        StartNuclearWar();
    }
}

Where is it?

like image 877
Pietro Avatar asked Jan 12 '13 14:01

Pietro


2 Answers

The bug is in the comment: for us the final \ character represents a directory, for the compiler it means that the following line is part of the current line. Of course every developer knows that, but in this case this detail is quite well hidden.

like image 74
Pietro Avatar answered Oct 31 '22 03:10

Pietro


Nice gotcha, MSDN calls this line splicing.

All lines ending in a backslash (\) and immediately followed by a newline character are joined with the next line in the source file forming logical lines from the physical lines. Unless it is empty, a source file must end in a newline character that is not preceded by a backslash.

like image 36
Steve Avatar answered Oct 31 '22 01:10

Steve