Why does the order of my #includes matter? (C ++)
I created a header file called "list_dec.h", put it in the "C: \ Headers" folder and set my compiler to include files from "C: \ Headers" so now I can do something like
#include<list_dec.h>
int main(){return(0);}
but when i try to do something like
#include<iostream>
#include<list_dec.h>
int main(){return(0);}
I am getting an error (not something specific, just a huge list of syntax errors in "list_dec.h" which I know is not real because I was able to compile it as both the main.cpp file and the .h file into a separate project). However, when I change the order so that "list_dec.h" is at the top:
#include<list_dec.h>
#include<iostream>
int main(){return(0);}
all errors disappear. So why does the order of the error matter?
NB: As far as I know, this happens when I use "list_dec.h" with all the header files, but the files that I am absolutely sure are found in:
#include<iostream>
#include<vector>
#include<time.h>
#include<stdlib.h>
EDIT: These are the errors I get when "list_dec.h" is below any other header:
c:\headers\list_dec.h(14) : error C2143: syntax error : missing ')' before 'constant'
c:\headers\list_dec.h(51) : see reference to class template instantiation 'list<T,limit>' being compiled
c:\headers\list_dec.h(14) : error C2143: syntax error : missing ';' before 'constant'
c:\headers\list_dec.h(14) : error C2059: syntax error : ')'
c:\headers\list_dec.h(14) : error C2238: unexpected token(s) preceding ';'
c:\headers\list_dec.h(69) : warning C4346: 'list<T,limit>::{ctor}' : dependent name is not a type
prefix with 'typename' to indicate a type
c:\headers\list_dec.h(69) : error C2143: syntax error : missing ')' before 'constant'
c:\headers\list_dec.h(69) : error C2143: syntax error : missing ';' before 'constant'
c:\headers\list_dec.h(69) : error C2988: unrecognizable template declaration/definition
c:\headers\list_dec.h(69) : error C2059: syntax error : 'constant'
c:\headers\list_dec.h(69) : error C2059: syntax error : ')'
c:\headers\list_dec.h(78) : error C2065: 'T' : undeclared identifier
c:\headers\list_dec.h(78) : error C2065: 'limit' : undeclared identifier
c:\headers\list_dec.h(78) : error C2065: 'T' : undeclared identifier
c:\headers\list_dec.h(78) : error C2065: 'limit' : undeclared identifier
c:\headers\list_dec.h(79) : error C2143: syntax error : missing ';' before '{'
c:\headers\list_dec.h(79) : error C2447: '{' : missing function header (old-style formal list?)
If it helps, these are the lines mentioned in errors (14, 69, 78 and 79):
Line 14: list(const T& NULL); (A constructor for "list" class)
Line 69: inline list<T, limit>::list(const T& NULL): (Definition for the constructor, also, the colon at the end is intentional, It part of the definion ie: void x(int n): VAR(n).)
Line 78: inline list<T, limit>::list(const list<T, limit>& lst) (def for the copy constructor)
Line 79: { (the begining of the list-copy contructor)
And many want to see the start of "list_dec.h":
template<class T, size_t limit>
class list
NB: These are not the first lines, but they are where I think the problem is that the lines before them are just an enum called "err".
EDIT: just a note, "list_dec.h" does not contain include, specifies ifdefs or anything preceded by "#". Besides the enumeration, it only contains the declaration of the list class and the function definitions of the list class.
In general, this should not be the case, but it might be possible for conflicting symbol definitions or preprocessor macros to end up confusing the compiler. Try to narrow down the size of the problem by removing chunks and including them from the conflicting header until you see what is causing it.
In response to the error messages you posted, the symbol is NULL
often implemented as a preprocessor macro for the number 0. This means that you can easily use it as a null pointer. Therefore this:
list(const T& NULL);
Can be converted to this syntax error by the preprocessor:
list(const T& 0);
Change the parameter name to something other than NULL
.
a source to share
Please note that here:
Line 14: list(const T& NULL); (A constructor for "list" class)
NULL
- this is the name of the standard macro - when the standard header file is included before list_dec.h, it will most likely define NULL
, which in turn will cause your code to look something like what the compiler does
list(const T& 0);
The constant 0
above makes the line ill-formed C ++. You can get more information by telling your compiler to generate a preprocessed output file.
a source to share