为什么#includes的顺序很重要? (C ++) [英] Why does the order of my #includes matter? (C++)

查看:163
本文介绍了为什么#includes的顺序很重要? (C ++)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个名为"list_dec.h"的头文件,并将其放置在文件夹"C:\ Headers"中,并将编译器设置为包括"C:\ Headers"中的文件,因此现在我可以执行操作喜欢

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);}

我得到一个错误(没有什么特别的,只是"list_dec.h"中的语法错误的列表很大,我知道这不是真实的,因为我已经能够将其编译为main.cpp文件和.h文件在单独的项目中).但是,当我更改订单时,"list_dec.h"位于顶部:

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?

注意:据我所知,当我对所有头文件使用"list_dec.h"时会发生这种情况,但是我绝对肯定会出现在以下文件中:

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>

这些是当"list_dec.h"位于任何其他标题下方时出现的错误:

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

如果有帮助,以下是错误中提到的行(14、69、78和79):

If it helps, these are the lines mentioned in the errors (14, 69, 78, and 79):

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

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

第78行:inline list<T, limit>::list(const list<T, limit>& lst) (def for the copy constructor)

第79行:{ (the begining of the list-copy contructor)

很多人都希望看到"list_dec.h"的开头:

And a lot of people want to see the beginning of "list_dec.h":

template<class T, size_t limit>
class list

注意:这些不是第一行,但我认为问题出在哪里,它们前面的行只是一个称为"err"的枚举.

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

仅需注意,"list_dec.h"不包含包含,定义,ifdef或任何以#"开头的内容.除枚举外,它仅包含列表"类声明和列表"类成员函数定义.

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.

为响应您发布的错误消息,符号NULL通常被实现为数字0的预处理器宏.这样,您可以轻松地将其用作空指针.因此:

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

将参数名称更改为NULL以外的其他名称.

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

这篇关于为什么#includes的顺序很重要? (C ++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆