struct的存储大小不为C++所知

use*_*900 0 c++ struct

当我这样做时,我得到以下错误

"错误:'mscp_commands'的存储大小未知"

struct command mscp_commands[]; /* forward declaration */
Run Code Online (Sandbox Code Playgroud)

后来我有:

struct command mscp_commands[] = {
 { "help",      cmd_help,       "show this list of commands"            },
 { "bd",        cmd_bd,         "display board"                         },
 { "ls",        cmd_list_moves, "list moves"                            },
 { "new",       cmd_new,        "new game"                              },
 { "go",        cmd_go,         "computer starts playing"               },
 { "test",      cmd_test,       "search (depth)"                        },
 { "quit",      cmd_quit,       "leave chess program"                   },
 { "sd",        cmd_set_depth,  "set maximum search depth (plies)"      },
 { "both",      cmd_both,       "computer plays both sides"             },
};
Run Code Online (Sandbox Code Playgroud)

前面以这种方式声明struct mscp_commands有什么问题?

命令结构先前已定义:

struct command {
    char *name;
    void (*cmd)(char*);
    char *help;
};
Run Code Online (Sandbox Code Playgroud)

Fle*_*exo 7

struct command mscp_commands[];定义不是声明(假设struct command已定义),但它不知道该点的存储大小,因为不知道元素的数量mscp_commands.这[]是明显不同的案例之一*.

你可以写:

extern struct command mscp_commands[];
Run Code Online (Sandbox Code Playgroud)

这确实是一个宣言.

  • @ user975900:这是因为你不能将非const char*绑定到c ++中的字符串文字.c ++与c不同.如果你想写c,也许写c,而不是c ++? (3认同)
  • @ user975900你需要将`char*`改为`const char*`. (3认同)
  • @ user975900 - 这是你的`struct command`的一个错误 - 如果你想使用像这样的字符串文字,你需要为`name`和`help`说`const char*`. (2认同)