【发布时间】:2009-05-14 21:10:17
【问题描述】:
我创建了一个名为“list_dec.h”的头文件,将它放在文件夹“C:\Headers”中,并将我的编译器设置为包含来自“C:\Headers”的文件,所以现在我可以做一些事情了喜欢
#include<list_dec.h>
int main(){return(0);}
但是当我尝试做类似的事情时
#include<iostream>
#include<list_dec.h>
int main(){return(0);}
我得到一个错误(不是任何具体的,只是“list_dec.h”中的一个巨大的语法错误列表,我知道这不是真的,因为我已经能够将它编译为 main.cpp 文件和.h 文件在一个单独的项目中)。但是,当我更改订单时,“list_dec.h”位于顶部:
#include<list_dec.h>
#include<iostream>
int main(){return(0);}
所有错误都消失了。那么为什么错误的顺序很重要呢?
注意:据我所知,当我将“list_dec.h”与所有头文件一起使用时会发生这种情况,但我绝对肯定它出现在的文件是:
#include<iostream>
#include<vector>
#include<time.h>
#include<stdlib.h>
编辑:当“list_dec.h”位于任何其他标题下方时,我会遇到以下错误:
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):
第 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”的开头:
template<class T, size_t limit>
class list
注意:这些不是第一行,但它们是我认为问题所在,它们之前的行只是一个名为“err”的枚举。
编辑:请注意,“list_dec.h”不包含包含、定义、ifdef 或任何以“#”开头的内容。除枚举外,仅包含“list”类声明和“list”类成员函数定义。
【问题讨论】:
-
你能贴出list_dec.h的开头吗?
-
您能否至少发布您遇到的前几个错误,这可能会有所启发。
-
类名是“list”,枚举名是“err”,其他都是list的数据成员或成员函数,我尝试的几乎所有其他头文件都会出现错误.另外,正如我所说,它仅在我将 #include
放在另一个 #include 之后发生,而不是在我之前放置它时发生。 -
让我们扭转局面:为什么不呢?