Thread: syntax error: enum and typedef struct

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    7

    syntax error: enum and typedef struct

    Hi,

    I'm trying to compile a project using gcc and I got the following error message:

    saw.h:52: error: syntax error before "conn_state"
    saw.h:52: warning: no semicolon at end of struct or union
    saw.h:58: error: syntax error before "}" token
    saw.h:58: warning: type defaults to ˜int" in declaration of "state_t"
    saw.h:58: warning: data definition has no type or storage class
    saw.h:60: error: syntax error before "s"
    saw.h:60: warning: type defaults to "int" in declaration of "s"
    saw.h:60: warning: data definition has no type or storage class

    Here's the code the errors are referring to:
    Code:
    enum conn_state {
    	CLOSED=0,
    	SYN_SENT,
    	SYN_RCVD,
    	ESTABLISHED,
    	FIN_SENT,
    	FIN_RCVD
    };
    
    typedef struct {
    	conn_state state;                               //line 52
    	uint8_t current_seqnum;
    	struct sockaddr_in other;
    	socklen_t otherLen;
    	int duplicate;
    	/* What should go here? */
    } state_t;
    
    extern state_t s;
    I tried checking online and in my books and I think I'm using the right syntax. Does anyone know what I'm doing wrong? Thanks!

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    use
    Code:
    typedef enum  {
    	CLOSED=0,
    	SYN_SENT,
    	SYN_RCVD,
    	ESTABLISHED,
    	FIN_SENT,
    	FIN_RCVD
    }conn_state;
    then You can use conn_state as a type;

    or just use
    Code:
    enum conn_state state;
    in your structure.
    Currently you try to define a variable state with the type that does not exist.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Your current code
    Code:
    enum anenum {};
    requires you to use the enum keyword when you're declaring an instance of it:
    Code:
    enum anenum theinstanceofanenum;
    With a typedef
    Code:
    typedef enum {} anenum;
    you have to leave out the enum keyword:
    Code:
    anenum theinstanceofanenum;
    For both (with or without the "enum"), use this:
    Code:
    typedef enum anenum {} anenum_t;
    
    enum anenum x;
    anenum_t y;
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  3. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 12:36 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM