免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 5354 | 回复: 5
打印 上一主题 下一主题

结构体的初始化问题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2004-12-06 19:53 |只看该作者 |倒序浏览
下面的程序结构体一个个初化有错误,而用加注释的代替是正确的,搞了好久也没看出来,请帮我看看,thanks.



  #include <iostream>;
  #include <string>;
  using namespace std;

static inline void do_open() {
  std::cout << "do_open called\n";
  }

  static inline void do_close() {
    std::cout << "do_close called\n";
  }

static inline void do_save() {
    std::cout << "do_save called\n";
  }

  static inline void do_quit() {
    exit(0);
  }

  struct cmd_info {
   char*  cmd;
   void (*funct)();
  };

   /*struct cmd_info cmd_list[]={

     {"open", do_open},

     {"close", do_close},

     {"save", do_save},

     {"quit", do_quit},

     {NULL, NULL}

  };  */
  struct cmd_info cmd_list[5];
   cmd_list[0].cmd="open";
   cmd_list[0].funct=do_open;

    cmd_list[1].cmd="close";
   cmd_list[1].funct=do_close;
   
   cmd_list[2].cmd="save";
   cmd_list[2].funct=do_save;
   
   cmd_list[3].cmd="quit";
   cmd_list[3].funct=do_quit;

   cmd_list[4].cmd=NULL;
   cmd_list[4].funct=NULL;
  
   

  
  static void do_cmd(const char *const cmd)
  {
     struct cmd_info *cur_cmd;
     cur_cmd = cmd_list;

     while ((strcmp(cur_cmd->;cmd, cmd) != 0) && cur_cmd != NULL)

     {            
           cur_cmd++;
            }
    if (cur_cmd == NULL) {
       std::cout << "Command not found\n";}
        else {
       cur_cmd->;funct();

        }

  }


void main()
  { char cmd[100];
    while (1) {
       std::cout << "Cmd: ";
       std::cin.getline(cmd, sizeof(cmd));
       do_cmd(cmd);
    }
  }

错误为:--------------------Configuration: c - Win32 Debug--------------------
Compiling...
c.cpp
E:\c.cpp(42) : error C2466: cannot allocate an array of constant size 0
E:\c.cpp(42) : error C2143: syntax error : missing ')' before '.'
E:\c.cpp(42) : error C2143: syntax error : missing ';' before '.'
E:\c.cpp(42) : error C2501: 'cmd_list' : missing storage-class or type specifiers
E:\c.cpp(42) : error C2371: 'cmd_list' : redefinition; different basic types
        E:\c.cpp(41) : see declaration of 'cmd_list'
E:\c.cpp(42) : error C2143: syntax error : missing ';' before '.'
E:\c.cpp(42) : error C2059: syntax error : ')'
E:\c.cpp(43) : error C2466: cannot allocate an array of constant size 0
E:\c.cpp(43) : error C2143: syntax error : missing ';' before '.'
E:\c.cpp(43) : error C2501: 'cmd_list' : missing storage-class or type specifiers
E:\c.cpp(43) : error C2371: 'cmd_list' : redefinition; different basic types
        E:\c.cpp(41) : see declaration of 'cmd_list'
E:\c.cpp(43) : error C2143: syntax error : missing ';' before '.'
E:\c.cpp(45) : error C2143: syntax error : missing ';' before '.'
E:\c.cpp(45) : error C2501: 'cmd_list' : missing storage-class or type specifiers
E:\c.cpp(45) : error C2369: 'cmd_list' : redefinition; different subscripts
        E:\c.cpp(41) : see declaration of 'cmd_list'
E:\c.cpp(45) : error C2143: syntax error : missing ';' before '.'
E:\c.cpp(46) : error C2143: syntax error : missing ';' before '.'
E:\c.cpp(46) : error C2501: 'cmd_list' : missing storage-class or type specifiers
E:\c.cpp(46) : error C2369: 'cmd_list' : redefinition; different subscripts
        E:\c.cpp(41) : see declaration of 'cmd_list'
E:\c.cpp(46) : error C2143: syntax error : missing ';' before '.'
E:\c.cpp(4 : error C2143: syntax error : missing ';' before '.'
E:\c.cpp(4 : error C2501: 'cmd_list' : missing storage-class or type specifiers
E:\c.cpp(4 : error C2369: 'cmd_list' : redefinition; different subscripts
        E:\c.cpp(41) : see declaration of 'cmd_list'
E:\c.cpp(4 : error C2143: syntax error : missing ';' before '.'
E:\c.cpp(49) : error C2143: syntax error : missing ';' before '.'
E:\c.cpp(49) : error C2501: 'cmd_list' : missing storage-class or type specifiers
E:\c.cpp(49) : error C2369: 'cmd_list' : redefinition; different subscripts
        E:\c.cpp(41) : see declaration of 'cmd_list'
E:\c.cpp(49) : error C2143: syntax error : missing ';' before '.'
E:\c.cpp(51) : error C2143: syntax error : missing ';' before '.'
E:\c.cpp(51) : error C2501: 'cmd_list' : missing storage-class or type specifiers
E:\c.cpp(51) : error C2369: 'cmd_list' : redefinition; different subscripts
        E:\c.cpp(41) : see declaration of 'cmd_list'
E:\c.cpp(51) : error C2143: syntax error : missing ';' before '.'
E:\c.cpp(52) : error C2143: syntax error : missing ';' before '.'
E:\c.cpp(52) : error C2501: 'cmd_list' : missing storage-class or type specifiers
E:\c.cpp(52) : error C2369: 'cmd_list' : redefinition; different subscripts
        E:\c.cpp(41) : see declaration of 'cmd_list'
E:\c.cpp(52) : error C2143: syntax error : missing ';' before '.'
E:\c.cpp(54) : error C2143: syntax error : missing ';' before '.'
E:\c.cpp(54) : error C2501: 'cmd_list' : missing storage-class or type specifiers
E:\c.cpp(54) : error C2369: 'cmd_list' : redefinition; different subscripts
        E:\c.cpp(41) : see declaration of 'cmd_list'
E:\c.cpp(54) : error C2143: syntax error : missing ';' before '.'
E:\c.cpp(55) : error C2143: syntax error : missing ';' before '.'
E:\c.cpp(55) : error C2501: 'cmd_list' : missing storage-class or type specifiers
E:\c.cpp(55) : error C2369: 'cmd_list' : redefinition; different subscripts
        E:\c.cpp(41) : see declaration of 'cmd_list'
E:\c.cpp(55) : error C2143: syntax error : missing ';' before '.'
Error executing cl.exe.

c.exe - 44 error(s), 0 warning(s)

在BC中编译错误提示为:cmd_list[0].cmd="open";两次赋值。

论坛徽章:
0
2 [报告]
发表于 2004-12-06 19:58 |只看该作者

结构体的初始化问题

你的不叫初始化,叫赋值,而结构和数组的赋值要写到函数内部吧

论坛徽章:
0
3 [报告]
发表于 2004-12-06 20:08 |只看该作者

结构体的初始化问题

对,是赋值,为什么结构体和数组的赋值要在函数内部?以前从没听说过。

论坛徽章:
0
4 [报告]
发表于 2004-12-06 20:23 |只看该作者

结构体的初始化问题

我觉得应该是这样的,其实所有的赋值都要在函数内部

初始化是编译时确定的,而赋值是运行的时候进行的,要运行肯定要放在函数内拉,这样产生的代码才会在代码段,初始化的数据都在数据段,没有办法运行吧

论坛徽章:
1
荣誉会员
日期:2011-11-23 16:44:17
5 [报告]
发表于 2004-12-06 21:49 |只看该作者

结构体的初始化问题

还有在main外赋值的?!!

论坛徽章:
1
荣誉版主
日期:2011-11-23 16:44:17
6 [报告]
发表于 2004-12-07 08:40 |只看该作者

结构体的初始化问题

你不用注释里面的话,赋值是语句,是会翻译成指令的,而这指令又不在任何一个函数内,所以不会执行,所以编译器就会认为是出错的。
而你用注释里的话,初始化的动作就是由编译器自动完成的了。
如果你想用原来那样赋值,可以写一个函数,然后在main里面调用就可以了。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP