Unexplainable Compiling Errors

We all agree that compiling errors are something very common while writing a program. Some of them can be detected by the compiler (mainly syntax errors) and some of them only during runtime. I thought the runtime errors were the most irritating and hard to detect ones until I got into this:

//graph.cpp
#include <iostream>
using namespace std;
 
int main() {
//----------Adjacency Matrix-------\\
	int matrix[2][2];
	matrix[0][0] = 0;
	matrix[0][1] = 1;
	matrix[1][0] = 1;
	matrix[1][1] = 1;
//----------------------------------\\
 
// code was continued here..
 
	return 0;
}

g++ graph.cpp
graph.cpp: In function ‘int main()’:
graph.cpp:8: error: ‘matrix’ was not declared in this scope

I would probably think that g++ had some kind of problem or I would rewrite the code from the start if I didn’t recall that something similar had occured to abresas. In a perfectly written code the compiler produced strange errors. He had also told me that he solved the problem by removing the comments! We couldn’t understand what exactly was going back then, but there is always a simple logical explanation for everything.

Do you remember a rule that everyone should follow while writing macros? The macros should all be in one line. That would make the code ugly though, so the following code:

#define ASSERT(x)if(! (x) ) {cout << "ERROR! Assert " << #x << "failed\n";cout << " on line " << __LINE__ << '\n';cout << " in file " << __FILE__ << '\n';} 

could also be written in this way:

#define ASSERT(x)\
 
        if(! (x) ) {\
 
             cout << "ERROR! Assert " << #x << "failed\n";\
 
             cout << " on line " << __LINE__ << '\n';\
 
             cout << " in file " << __FILE__ << '\n';\
 
        }

Apparently, ‘\’ is used for connecting lines apart from being an escape character.Therefore:

int main() {
//----------Adjacency Matrix-------\\
	int matrix[2][2];
	matrix[0][0] = 0;

is interpreted by the compiler as:

int main() {
//----------Adjacency Matrix-------\\int matrix[2][2];
	matrix[0][0] = 0;

and thus leaves us with an undefined variable.
Imagine having to face such an error while being on a contest. Yikes!

One Response to “Unexplainable Compiling Errors”

  1. dionyziz says:

    A very interesting problem indeed! I did not know that \ at the end of a comment line was interpreted the same way as at the end of a normal line. Thanks for the insightful post :)

Leave a Reply