两个结构的顺序

发布于 2024-11-29 00:36:40 字数 478 浏览 2 评论 0原文

我有这两种结构...

typedef struct{
    MY_SECOND_STRUCT  s1;
}MY_FIRST_STRUCT;


typedef struct{
    int s1;
}MY_SECOND_STRUCT;

我更喜欢这个顺序,我不想切换它们。 但编译器目前不知道 MY_SECOND_STRUCT 并且出现错误

错误:“MY_SECOND_STRUCT”之前应有说明符限定符列表

我尝试将声明添加到顶部

struct MY_SECOND_STRUCT;

并将定义更改为

typedef struct{
    struct MY_SECOND_STRUCT  s1;
}MY_FIRST_STRUCT;

但没有帮助。

I have these two structures...

typedef struct{
    MY_SECOND_STRUCT  s1;
}MY_FIRST_STRUCT;


typedef struct{
    int s1;
}MY_SECOND_STRUCT;

I prefer this order, I dont want to switch them.
But compiler dont know MY_SECOND_STRUCT at the moment and I get error

error: expected specifier-qualifier-list before 'MY_SECOND_STRUCT'

I´ve tried add declaration to the top

struct MY_SECOND_STRUCT;

also change definition to

typedef struct{
    struct MY_SECOND_STRUCT  s1;
}MY_FIRST_STRUCT;

but it didnt help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

耀眼的星火 2024-12-06 00:36:40

我更喜欢这个顺序,我不想改变它们。

你必须切换它们。

如果 MY_FIRST_STRUCT 具有 MY_SECOND_STRUCT 类型的成员变量,则 MY_SECOND_STRUCT 必须在定义 之前定义且完整(而不仅仅是声明和不完整) >MY_FIRST_STRUCT

I prefer this order, I dont want to switch them.

You have to switch them.

If MY_FIRST_STRUCT has a member variable of type MY_SECOND_STRUCT then MY_SECOND_STRUCT must be defined and complete (not just declared and incomplete) before the definition of MY_FIRST_STRUCT.

对风讲故事 2024-12-06 00:36:40

该命令是不可能的。你必须切换它们。

但是,如果您将成员声明为指针,则不需要切换:

struct MY_SECOND_STRUCT; //forward declaration is required though

typedef struct{
    MY_SECOND_STRUCT  * s1;  //now its a pointer
}MY_FIRST_STRUCT;

或者,在 C++ 中,您可以将模板用作:

template<typename MY_SECOND_STRUCT>
struct MY_FIRST_STRUCT_T
{
    MY_SECOND_STRUCT  s1;
};

struct MY_SECOND_STRUCT
{
    int s1;
};

当您想使用 MY_FIRST_STRUCT 时,只需使用此 typedef 即可code>:

typedef MY_FIRST_STRUCT_T<MY_SECOND_STRUCT> MY_FIRST_STRUCT;

立即使用MY_FIRST_STRUCT。 :-)

That order is not possible. You have to switch them.

However, if you declare the member as pointer, then switching is not required:

struct MY_SECOND_STRUCT; //forward declaration is required though

typedef struct{
    MY_SECOND_STRUCT  * s1;  //now its a pointer
}MY_FIRST_STRUCT;

Or, in C++ you can use template as:

template<typename MY_SECOND_STRUCT>
struct MY_FIRST_STRUCT_T
{
    MY_SECOND_STRUCT  s1;
};

struct MY_SECOND_STRUCT
{
    int s1;
};

And when you want to use MY_FIRST_STRUCT, just use this typedef:

typedef MY_FIRST_STRUCT_T<MY_SECOND_STRUCT> MY_FIRST_STRUCT;

Use MY_FIRST_STRUCT now. :-)

沙沙粒小 2024-12-06 00:36:40

当您创建第一个结构时,编译器尚未遇到第二个结构。它需要知道 MY_SECOND_STRUCT 位是多少,因为那时它需要决定 MY_FISRT_STRUCT 的大小以及如何为其分配内存。简单地向前声明 struct MY_FIRST_STRUCT 是不够的,这不会告诉编译器该结构的大小或内容。如果您使用指向结构的指针,则前向声明会起作用,但在尝试包含实际结构时,前向声明还不够。

您唯一真正的选择是将 MY_SECOND_STRUCT 移至 MY_FIRST_STRUCT 上方,或者使 MY_FIRST_STRUCT 采用指针。乍一看,这可能看起来很奇怪,但对此我们无能为力。

At the time you cerate your first struct the compiler hasn't come across the second struct yet. It needs to know how bit MY_SECOND_STRUCT is because at that time it needs to decide how big to make MY_FISRT_STRUCT and how to lay out the memory for it. Simply forward declaring struct MY_FIRST_STRUCT is not enough, that doesn't tell the compiler about the size or contents of the struct. Forward declaring would work if you were using a pointer to a struct, but it's not enough when trying to include the actual structure.

Your only real options are to move MY_SECOND_STRUCT up above MY_FIRST_STRUCT or to make MY_FIRST_STRUCT takes a pointer. It might seem strange at first, but there's not much that can be done about it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文