Thread: Parse error has me stumped - possible issue with header files?

  1. #1
    Feversaint
    Guest

    Question Parse error has me stumped - possible issue with header files?

    I think my errors and code will explain the issue fairly well (my thoughts at the bottom):

    When I compile:
    Code:
    In file included from mylib/physics.h:21,
                     from mylib/level.h:26,
                     from mylib/gameload.h:14,
                     from mylib/game.h:17,
                     from mylib.h:4,
                     from main.c:13:
    mylib/gameupdate.h:46: error: parse error before '*' token
    In file included from mylib/physics.h:21,
                     from mylib/level.h:26,
                     from mylib/level.c:14:
    mylib/gameupdate.h:46: error: parse error before '*' token
    In file included from mylib/physics.h:21,
                     from mylib/level.h:26,
                     from mylib/gameload.h:14,
                     from mylib/gameload.c:14:
    mylib/gameupdate.h:46: error: parse error before '*' token
    In file included from mylib/physics.h:21,
                     from mylib/level.h:26,
                     from mylib/gamedraw.h:24,
                     from mylib/gamedraw.c:14:
    mylib/gameupdate.h:46: error: parse error before '*' token
    In file included from mylib/physics.h:21,
                     from mylib/level.h:26,
                     from mylib/gameload.h:14,
                     from mylib/game.h:17,
                     from mylib/game.c:14:
    mylib/gameupdate.h:46: error: parse error before '*' token
    The offending code from gameupdate.h
    Code:
    #ifndef GAMEUPDATE_H
    #define GAMEUPDATE_H
    
    #include <stdio.h> 
    #include <time.h>
    
    #include "level.h" /* <-- Level header included here! */
    
    #include "gameobj.h"
    #include "entity.h"
    #include "worldgeo.h"
    
    #include "graphics.h"
    #include "bool.h"
    #include "artasset.h"
    #include "physics.h"
    #include "linklist_go.h"
    #include "linklist_aa.h"
    #include "gameState.h"
    #include "custmath.h"
    
    ...
    
    void Update(Level *level, /* <-- Errors are from here! */
      gameState *currentState,
      LinkedList_GO *layers,
      bool changeTracker[SCREEN_HEIGHT][SCREEN_WIDTH],
      Entity* player);
    
    ...
    
    #endif
    Definition of Level in level.h
    Code:
    #ifndef LEVEL_H
    #define LEVEL_H
    
    #include "gameobj.h"
    #include "entity.h"
    #include "worldgeo.h"
    
    #include "graphics.h"
    #include "bool.h"
    #include "artasset.h"
    #include "physics.h"
    #include "linklist_go.h"
    #include "linklist_aa.h"
    #include "gameState.h"
    #include "custmath.h"
    
    ...
    
    typedef struct Level /* <-- Definition here! */
    {
      char name[32];
      
      /* 
        Layers:
            0 - Background (Map)
            1 - Geometry
            2 - Entities
            3 - HUD
      */
      LinkedList_GO layers[4];
      LinkedList_AA artBank;
      Entity player;
      WorldGeometry map;
      
    } Level;
    
    ...
    
    #endif
    From my understanding this parse error would indicate that the compiler doesn't know what "Level" is when it reaches the line where the error is. Does it matter that I have "level.h" including "gameupdate.h" and "gameupdate.h" including "level.h"? If so - how could I resolve this (I have need to use level in gameupdate, and I have one function in gameupdate that level needs), if not - any ideas? I'm stumped ...

    Thanks a ton in advance for your time and assistance!

    EDIT: I forgot to mention - all my files except main.c are in a folder named mylib in the same directory as main.c.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    I know the compiler says line 46, but sometimes it can get confused when you have a function definition spanning several lines. Perhaps try adding temporary prototypes to isolate which type in the parameter list is causing the problem:
    Code:
    void foo(Level *);
    void bar(gameState *);
    // etc
    You may find that it's not actually the definition of Level that it's missing. I have a slight hunch it might be gameState, since all of your other types start with a capital letter and that one doesn't. Check it against what's in gameState.h.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Feversaint View Post
    Does it matter that I have "level.h" including "gameupdate.h" and "gameupdate.h" including "level.h"?
    Very likely it does.

    how could I resolve this (I have need to use level in gameupdate, and I have one function in gameupdate that level needs)
    If this function's parameters include a type defined in level.h, but level.h includes gameupdate.h before that type is declared, that is a problem.

    The simplest solution may be to define the type in level.h before you include gameupdate.h (the #include statements do not have to be before everything else). The best solution is probably to think a little bit about your headers and their purpose. You do not need to have a one to one .c -> .h relationship; you can use additional headers (eg, declaring and defining common types) that are included in multiple .c or .h files (using include guards, of course). That kind of a header will not have any function prototypes in it, so it does not need a corresponding .c; all it does is declare and define types, constants, #defines, and enums.
    Last edited by MK27; 10-19-2011 at 12:47 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Feversaint
    Guest
    Thanks for the advice! Not working quite yet, but I am going to reconsider my design (and I did verify that level is the problem thanks to the other suggestion).

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The number of possible causes of your problem is huge.

    The compiler only reports errors at the point it detects a problem. In practice, if a parse error is reported at line 46, the cause can be in any code up to and including line 46. If line 46 is a multi-line statement (as it is in your case) the cause can be after line 46 (within the same statement).

    The most common causes are misplaced semi-colons, missing brackets or braces, using a type name where the compiler expects a variable name (or vice versa).

    It is also possible the cause of your problem is a macro definition in one of your header files. A common symptom of a macro containing a misplaced semi-colon, for example, is a parse error when the macro is later used but there will often be no error reported for the actual definition of the macro. That is a result of how the preprocessor works (by text substitution, after which the modified code is interpreted by the compiler).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well since you have over a dozen header files, I'm guessing a similar number of source files as well.

    In which case, it's time to investigate Revision control - Wikipedia, the free encyclopedia

    In particular, it would help you solve the current problem.
    It would be able to do this, by allowing you to compare the last known good version with the one you have.

    Searching through a few pages of diffs of the most recent edits would make this task a lot easier.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Code:
    typedef struct Level /* <-- Definition here! */  /* tag name */
    {
      char name[32];
      
      /* 
        Layers:
            0 - Background (Map)
            1 - Geometry
            2 - Entities
            3 - HUD
      */
      LinkedList_GO layers[4];
      LinkedList_AA artBank;
      Entity player;
      WorldGeometry map;
      
    } Level; /* type name */
    Some old compilers needed the tag name and type name to be different.

    Tim S.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by stahta01 View Post
    Code:
    typedef struct Level /* <-- Definition here! */  /* tag name */
    {
      char name[32];
      
      /* 
        Layers:
            0 - Background (Map)
            1 - Geometry
            2 - Entities
            3 - HUD
      */
      LinkedList_GO layers[4];
      LinkedList_AA artBank;
      Entity player;
      WorldGeometry map;
      
    } Level; /* type name */
    Some old compilers needed the tag name and type name to be different.

    Tim S.
    New ones too...
    In Pelles, I get a "redefinition" error if they're the same...
    In fact it is an error.

  9. #9
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    It is not an error according to the C standard: struct type names and typedef names belong in different name spaces. However, it is one of those things that a fair percentage of C compilers, old and new, get wrong and therefore misdiagnose.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multiple Classes And Header Files error does not name a type
    By rainmanddw in forum C++ Programming
    Replies: 2
    Last Post: 09-28-2006, 11:17 AM
  2. Error Handling and Header Files
    By the pooper in forum C Programming
    Replies: 10
    Last Post: 01-03-2005, 01:40 AM
  3. error with 2 header files calling each other
    By paperbox005 in forum C++ Programming
    Replies: 3
    Last Post: 10-06-2004, 02:36 AM
  4. Linking Error Whenever I Tried To Use Header Files
    By javacvb in forum C++ Programming
    Replies: 5
    Last Post: 12-16-2003, 11:46 AM
  5. Parse Error that has me stumped
    By readerwhiz in forum C Programming
    Replies: 3
    Last Post: 05-28-2002, 09:32 PM

Tags for this Thread