C+ Array of struct throws error that the types are incomplete (Linux command options parsing)

时间:2021-11-14 21:27:39

I have the following array[] struct:

我有以下array []结构:

const struct options long_options[4] = {
    { "help", 0, NULL, "h" },
    { "output", 1, NULL, "o" },
    { "verbose", 0, NULL, "v" },
    { NULL,  0, NULL, NULL }
};

But it (CodeBlocks on Linux) throws this error:

但它(Linux上的CodeBlocks)抛出此错误:

elements of array ‘const options long_options [4]’ have incomplete type|

How should I solve this error?

我该如何解决这个错误?

UPDATE

I want to pass it to getopt_long() in getopt.h for Linux command parsing.

我想将它传递给getopt.h中的getopt_long()以进行Linux命令解析。

So, I have already: #include <getopt.h>

所以,我已经:#include

1 个解决方案

#1


6  

The compiler doesn't know what the struct options look like, so you need to tell it.

编译器不知道struct选项是什么样的,所以你需要告诉它。

It seems like you're trying to use getopt_long(). If this is true, to solve your issue you simply need to add #include <getopt.h> to the top of your file. See here

看起来你正在尝试使用getopt_long()。如果是这样,要解决您的问题,您只需将#include 添加到文件的顶部即可。看这里

Inside getopt.h there is a declaration that goes something like this

在getopt.h里面有一个类似这样的声明

struct option {
    const char *name;
    int has_arg;
    int *flag;
    int val;
};

so when you #include it, the compiler knows what struct option looks like and is able to initialize your array.

所以当你#include它时,编译器知道什么是struct选项,并且能够初始化你的数组。

EDIT: I missed the typo on your original post. The first line of the code you posted should be

编辑:我错过了原帖上的拼写错误。您发布的代码的第一行应该是

const struct option long_options[4] = ...

rather than

const struct options long_options[4] = ...

#1


6  

The compiler doesn't know what the struct options look like, so you need to tell it.

编译器不知道struct选项是什么样的,所以你需要告诉它。

It seems like you're trying to use getopt_long(). If this is true, to solve your issue you simply need to add #include <getopt.h> to the top of your file. See here

看起来你正在尝试使用getopt_long()。如果是这样,要解决您的问题,您只需将#include 添加到文件的顶部即可。看这里

Inside getopt.h there is a declaration that goes something like this

在getopt.h里面有一个类似这样的声明

struct option {
    const char *name;
    int has_arg;
    int *flag;
    int val;
};

so when you #include it, the compiler knows what struct option looks like and is able to initialize your array.

所以当你#include它时,编译器知道什么是struct选项,并且能够初始化你的数组。

EDIT: I missed the typo on your original post. The first line of the code you posted should be

编辑:我错过了原帖上的拼写错误。您发布的代码的第一行应该是

const struct option long_options[4] = ...

rather than

const struct options long_options[4] = ...