user975900
user975900

Reputation: 77

Storage size of struct isn't known C++

I get this following error when I do this

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

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

Later on I have:

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

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

Upvotes: 0

Views: 4984

Answers (2)

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 *.

You could however write:

extern struct command mscp_commands[];

which would indeed be a declaration.

Upvotes: 7

tune2fs
tune2fs

Reputation: 7705

With forward declarations the compiler cannot calculate the size of the object. Therefore the error message.

See also this link

Upvotes: 1

Related Questions