简体   繁体   中英

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) 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.

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 .

Note that here:

Line 14: list(const T& NULL); (A constructor for "list" class)

NULL is the name of standard macro - when a standard header file is included before list_dec.h it will most likely cause NULL to be defined which will in turn cause your code to look something like this to the compiler:

list(const T& 0);

The constant 0 above makes the line ill-formed C++. You might get more information by instructing your compiler to produce preprocessed output file.

大概list_dec.h正在运行在其他标头(或它们依次包含的某些标头)中定义的宏中-很难说是哪个,而不会看到第一条错误消息和list_dec.h的相关部分!

The actual errors would give a more specific clue, bt it means there's something in your include file that is screwing up the scan for the next one. The most common thing would be some kind of unclude #-directive, like a #if missing its #endif .

If the errors are random in nature, it could be a missing semi colon. The compiler will usually halt on that, but on occasion you get "lucky".

Otherwise, conflicting names or defines. Do you have anything named std for example?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM