Go Down

Topic: lont vs long (Read 380 times) previous topic - next topic

Kitep

I don't know if this is really a bug, but it bugged me :)

I was working on a library.  In the .h file, I wanted to declare a variable of type long, eg

 long StartMillisRead;

but instead I typed lont, eg

 lont StartMillisRead;

The compiler didn't report an error in the .h file like I would have expected, but instead reported it in the .cpp file when I tried using the variable.  While the important thing is that it showed an error, any reason it didn't say something like "unknown variable type" in the .h file?



kg4wsv

#1
Jun 22, 2009, 02:25 pm Last Edit: Jun 22, 2009, 02:25 pm by kg4wsv Reason: 1
You shouldn't declare variables in a header file.  headers are for non-executable stuff.

If you're writing a library, you should define the variable in the header file (e.g. extern int myGlobalVariable; ), and declare it in the .c (or .cpp) file (int myGlobalVariable;).

Now that that's out of the way, the strangeness you see is because .h files are consumed by the preprocessor before the compiler actually runs, so any errors in the .h file will be reported when the compiler processes the source code file than #includes the header.  Header files are not actually compiled directly by the compiler.

Compilers have advanced in how they report error messages, so hopefully there is a clue to the actual location of the error near the error message.

-j

Go Up