Thread: struct redefinition

  1. #1
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463

    struct redefinition

    the following code is the header file for the game/2d engine I'm making. When I build my program I get the error message that in the first function definition for collision() that the person struct is being redefined. I looked to make sure my function definition was right, and even tried looking in the K&R book. Maybe I'm getting too tired, but I couldnt find anything that would help the error message make sense of what I did wrong.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <allegro.h>
    
    #define ALLEGRO_USE_CONSOLE
    #define BULLET_VEL 3
    #define DEAD 0
    #define DYING -1
    #define ALIVE 1
    #define LEFT 0
    #define RIGHT 1
    #define PBULLETS 8
    #define EBULLETS 32
    
    struct person{
                    int x,y;
                    int width, height;
                    float x_vel, y_vel;
                    int alive;
                    int direction;
                    BITMAP *sprite;				
    };
    
    struct bullet{
                    int x,y;
                    int width, height;
                    float vel;
                    bool exists;
                    int num_bullets;		
    };
    
    
    
    bool collision(struct* person,struct* person);
    bool fire_bullet(struct* person,struct* bullet);
    void frames_per_sec(void);
    bool hit(struct* person);
    void update_bullets(void);
    Last edited by Draco; 07-26-2004 at 01:38 AM.

  2. #2
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    Maybe it's because you're using the same argument name in the function declaration.

    change this:
    bool collision(struct* person,struct* person);

    to this:
    bool collision(struct* personA,struct* personB);

    or something of the sort.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > bool collision(struct* person,struct* person);
    Don't you mean

    bool collision(struct person * me, struct person *you);
    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.

  4. #4
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    Salem, that's how I had it originally, but I got those error messages, and moving them to how I had posted took some away.

  5. #5
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    Could you post the exact error message. If you take Salem's advice, the prototypes should be right.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  6. #6
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    Code:
    c:\program files\microsoft visual studio\myprojects\action game 3\actiongame3.h(34) : error C2371: 'person' : redefinition; different basic types
            c:\program files\microsoft visual studio\myprojects\action game 3\actiongame3.h(34) : see declaration of 'person'
    This error message is there no matter if the prototype is correct or not. I also noticed that there has never been a similar error for the bullet structure in my fire_bullet() function.

  7. #7
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    That usually means you are trying to define person twice, which is invalid. Are you including a header file twice inadvertantly? Make sure there is no way that person can be defined twice.
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

  8. #8
    Quote Originally Posted by Draco
    Code:
    struct person{
    <...>
    };
    
    struct bullet{
    <...>
    };
    
    <...>
    bool collision(struct* person,struct* person);
    bool fire_bullet(struct* person,struct* bullet);
    bool hit(struct* person);
    <...>
    The function prototypes are buggy. 'struct' is not a struct. You need a tag:
    'struct person'
    'struct bullet'
    Code:
    bool collision(struct person* ,struct person*);
    bool fire_bullet(struct person*,struct bullet*);
    bool hit(struct person*);
    Last edited by Emmanuel Delaha; 07-26-2004 at 12:30 PM. Reason: cut
    Emmanuel Delahaye

    "C is a sharp tool"

  9. #9
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    well I could tell that my prototypes were bad....but thanks, when I changed it to that it fixed it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting from C to C++
    By Taka in forum C++ Programming
    Replies: 5
    Last Post: 04-08-2009, 02:16 AM
  2. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  3. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  4. What's wrong with my search program?
    By sherwi in forum C Programming
    Replies: 5
    Last Post: 04-28-2006, 09:57 AM
  5. Function validation.
    By Fhl in forum C Programming
    Replies: 10
    Last Post: 02-22-2006, 08:18 AM