我不明白我在这个编程代码中得到的错误 [英] I don't understand the error i get in this programming code

查看:158
本文介绍了我不明白我在这个编程代码中得到的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直得到这个错误,我使用VS 2013.我下载了MUD ROM 2.4b6的源代码,当我去编译这是我得到的。



错误5错误C2109:表达式必须是指向完整对象类型的指针



spec_table in:

  fun == spec_table [cmd] .function 

这是它给我的问题的代码。我完全不知道在哪里? Btw spec_table是在代码最顶部的同一个文件上定义的。

  / * OLC已插入* / 
/ ******** **************************************************** *******************
名称:spec_string
目的:给定一个函数,返回相应的名称。
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;
}

spec_table定义在文件的最顶部,只是一个代码段,因为整个事情太大了。

  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},



是头文件中spec_type的声明。

  struct spec_type 
{
char * name; / *特殊函数名* /
SPEC_FUN *函数; / *函数* /
};

这是SPEC_FUN的声明

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

这是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 *受影响;
NOTE_DATA * pnote;
OBJ_DATA * carry;
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 play;
int lines; / * for 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添加到pad向量* /
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;
长零件;
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;
};


解决方案

现在我们有了我们需要的代码...

  struct spec_type 
{
char * name; / *特殊函数名* /
SPEC_FUN *函数; / *函数* /
};

我假设 function 函数指针。你不能索引到一个函数指针,它没有意义,因为这些指针不是一个完整的类型。例如,你不能接受 sizeof 一个函数类型,那么编译器如何知道如何获得下一个元素呢?



这将是错误的; function [0] 永远不会是 char 类型。您是否要检查名称字段是否为空字符?



当索引到指针类型时,真正发生的是这样的:



<$> p $ p> p [n] = *(p + n)= *(p增加n个字节的大小)

看起来你只是想获得函数的字符串表示。尝试这样:

  //你应该返回一个const char *,而不是一个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;
}


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;
};

解决方案

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;
}

这篇关于我不明白我在这个编程代码中得到的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆