views:

448

answers:

5

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.

+4  A: 

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.

1800 INFORMATION
+2  A: 

Presumably list_dec.h is running into a macro that's defined in those other headers (or some headers they in turn include) -- hard to say which one without seeing the first error message and the relevant part of list_dec.h!

Alex Martelli
+1  A: 

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.

Charlie Martin
+1  A: 

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?

+2  A: 

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.

Bojan Resnik
I tried using NULL for that purpose before, but the compiler told me the NULL was undefined, so I used it here. Also, wouldn't that appear in a compiler error when I compiled "list_dec.h" itself? Because "list_dec.h" itself has no errors, the errors only occur when I put it below another #include.
Keand64
If you do not include standard headers NULL is undefined. If you include standard headers then NULL will be defined as 0. That is the reason it is better to use 0 for a null pointer constant (null_ptr will be a reserved word in the upcoming standard IIRC)
David Rodríguez - dribeas
NULL is not defined by the compiler, but by <cstddef> standard header. Do not use the names of standard macros for your own identifiers.
Bojan Resnik