Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the order of my #includes matter? (C++)

I've created a header file called "list_dec.h", put it in a folder "C:\Headers", and set my compiler to include files from "C:\Headers", so now I can do things 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 get an error (not anything specific, just a huge list of syntax errors in "list_dec.h", which I know aren't real because I've been able to compile it as both a main.cpp file and a .h file in a separate project). However, when I change to order so "list_dec.h" is on top:

#include<list_dec.h>
#include<iostream>
int main(){return(0);}

all of the errors go away. So why does the order of the error matter?

NB: As far as I know, this occurs when I use "list_dec.h" with all header files, but the files I'm absolutely positive it occurs in are:

#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 the 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 a lot of people want to see the beginning of "list_dec.h":

template<class T, size_t limit>
class list

NB: These aren't the first lines, but they're where I think the problem is, the lines before them are simply an enumeration called "err".

EDIT: Just a note, "list_dec.h" contains no includes, defines, ifdefs, or anything precede with a '#'. Besides the enumeration, it only contains the "list" class declaration and the "list" class member function definitions.


1 Answers

Generally speaking it should not, however it may be possible for there to be conflicting definitions of symbols or preprocessor macros that end up confusing the compiler. Try to narrow down the size of the problem by removing pieces and includes from the conflicting header until you can see what is causing it.

In response to the error messages you posted, the symbol NULL is often implemented as a preprocessor macro for the number 0. This is so that you can easily use it as a null pointer. Therefore this:

list(const T& NULL);

Could be converted into this syntax error by the preprocessor:

list(const T& 0);

Change the name of the parameter to something other than NULL.

like image 53
1800 INFORMATION Avatar answered Sep 08 '25 00:09

1800 INFORMATION