gpt4 book ai didi

c++ - 结构的存储大小未知 C++

转载 作者:太空宇宙 更新时间:2023-11-04 15:08:37 25 4
gpt4 key购买 nike

执行此操作时出现以下错误

“错误:‘mscp_commands’的存储大小未知”

struct command mscp_commands[]; /* forward declaration */

稍后我有:

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

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

command struct 定义较早:

struct command {
char *name;
void (*cmd)(char*);
char *help;
};

最佳答案

struct command mscp_commands[]; 是定义而不是声明(假设定义了 struct command),但它不知道此时的存储大小,因为 mscp_commands 中的元素数量未知。这是 []* 明显不同的情况之一。

但是你可以这样写:

extern struct command mscp_commands[];

这确实是一个声明。

关于c++ - 结构的存储大小未知 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7772897/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com