本文介绍了结构的存储大小未知 C++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I get this following error when I do this

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

"error: storage size of 'mscp_commands' isn't known"

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 有什么问题?

What's wrong with forward declaring the struct mscp_commands that way?

前面已经定义了命令结构:

The command struct is defined earlier:

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

推荐答案

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

struct command mscp_commands[]; is a definition not a declaration (assuming struct command is defined), but it doesn't know the storage size at that point, because the number of elements in mscp_commands isn't known. It's one of the cases where [] is notably different to *.

不过你可以写:

extern struct command mscp_commands[];

这确实是一个声明.

这篇关于结构的存储大小未知 C++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 04:17