Uys of Spades
Uys of Spades

Reputation: 171

I don't understand the error i get in this programming code

I keep getting this error, and I am using VS 2013. I downloaded the source code for MUD ROM 2.4b6 and when i go to compile this is what i get.

Error 5 error C2109: expression must be a pointer to a complete object type

the error is for the spec_table in:

fun == spec_table[cmd].function 

and this is the code that its giving me the problem. I have absolutely no idea where to being? Btw spec_table was defined on the same file at the very top of the code. Can anyone please shed some insight?

/* OLC Inserted */
/*****************************************************************************
 Name:      spec_string
 Purpose:   Given a function, return the appropriate name.
 Called by: <???>
 ****************************************************************************/
char *spec_string( SPEC_FUN *fun )  /* OLC */
{
int cmd;

for ( cmd = 0; spec_table[cmd].function[0] != '\0'; cmd++ )
if ( fun == spec_table[cmd].function )
    return spec_table[cmd].name;

return 0;
}

the spec_table is defined at the very top of the file with this section: It is just a snippet because the whole thing is too large.

const   struct  spec_type    spec_table[] =
{
    {   "spec_breath_any",          spec_breath_any         },
    {   "spec_breath_acid",         spec_breath_acid        },
    {   "spec_breath_fire",         spec_breath_fire        },
    {   "spec_breath_frost",        spec_breath_frost       },
    {   "spec_breath_gas",          spec_breath_gas         },
    {   "spec_breath_lightning",    spec_breath_lightning   },  

here is the declaration for spec_type in a header file.

struct spec_type
{
char *  name;           /* special function name */
SPEC_FUN *  function;       /* the function */
};

this is the declaration for SPEC_FUN

typedef bool SPEC_FUN   args( ( CHAR_DATA *ch ) );

this is the declaration for CHAR_DATA

    struct  char_data
{
CHAR_DATA *         next;
CHAR_DATA *         next_in_room;
CHAR_DATA *         master;
CHAR_DATA *         leader;
CHAR_DATA *         fighting;
CHAR_DATA *         reply;
CHAR_DATA *         pet;
MEM_DATA *          memory;
SPEC_FUN *          spec_fun;
MOB_INDEX_DATA *    pIndexData;
DESCRIPTOR_DATA *   desc;
AFFECT_DATA *       affected;
NOTE_DATA *         pnote;
OBJ_DATA *          carrying;
OBJ_DATA *          on;
ROOM_INDEX_DATA *   in_room;
ROOM_INDEX_DATA *   was_in_room;
AREA_DATA *         zone;
PC_DATA *           pcdata;
GEN_DATA *          gen_data;
bool                valid;
char *              name;
long                id;
sh_int              version;
char *              short_descr;
char *              long_descr;
char *              description;
char *              prompt;
char *              prefix;
sh_int              group;
sh_int              clan;
sh_int              sex;
sh_int              class;
sh_int              race;
sh_int              level;
sh_int              trust;
int                 played;
int                 lines;  /* for the pager */
time_t              logon;
sh_int              timer;
sh_int              wait;
sh_int              daze;
sh_int              hit;
sh_int              max_hit;
sh_int              mana;
sh_int              max_mana;
sh_int              move;
sh_int              max_move;
long                gold;
long                silver;
int                 exp;
long                act;
long                comm;   /* RT added to pad the vector */
long                wiznet; /* wiz stuff */
long                imm_flags;
long                res_flags;
long                vuln_flags;
sh_int              invis_level;
sh_int              incog_level;
long                affected_by;
sh_int              position;
sh_int              practice;
sh_int              train;
sh_int              carry_weight;
sh_int              carry_number;
sh_int              saving_throw;
sh_int              alignment;
sh_int              hitroll;
sh_int              damroll;
sh_int              armor[4];
sh_int              wimpy;
/* stats */
sh_int              perm_stat[MAX_STATS];
sh_int              mod_stat[MAX_STATS];
/* parts stuff */
long                form;
long                parts;
sh_int              size;
char*               material;
/* mobile stuff */
long                off_flags;
sh_int              damage[3];
sh_int              dam_type;
sh_int              start_pos;
sh_int              default_pos;
};

Upvotes: 2

Views: 152

Answers (1)

Ed Swangren
Ed Swangren

Reputation: 124642

Now that we have the code we need...

struct spec_type
{
    char *  name;           /* special function name */
    SPEC_FUN *  function;       /* the function */
};

I'm assuming that function is a function pointer. You cannot index into a function pointer, it makes no sense because these pointers do not poijnt to a complete type. For example, you cannot take the sizeof a function type, so how would the compiler know how to get to the next element?

It would be wrong anyway; function[0] would never be of the type char. Did you mean to check the name field for a null character? That would make more sense.

When you index into a pointer type, what's really going on is this:

p[n] = *(p + n) = *(p incremented by sizeof n bytes)

It looks like you're just trying to get the string representation of the function. Try this:

// you should be returning a const char*, not a char*
const char *spec_string( SPEC_FUN *fun )  /* OLC */
{
    int cmd;
    static const int size = sizeof spec_table / sizeof spec_table[0];
    for (cmd = 0; cmd < size; cmd++) {
        if (fun == spec_table[cmd].function) {
            return spec_table[cmd].name;
        }
    }

    return 0;
}

Upvotes: 1

Related Questions