为什么无法解决此类型? [英] Why can this type not be resolved?

查看:94
本文介绍了为什么无法解决此类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的C头文件中包含以下代码:

I have the following code in my C header file:

typedef struct mb32_packet_t {
  uint8_t compid;
  uint8_t servid;
  uint8_t payload[248];
  uint8_t checksum;
} __attribute__((packed)) mb32_packet_s;

请执行以下操作:

struct mb32_packet_t packet;

使用此功能时:

mb32_packet_t packet;

我得到:

Type 'mb32_packet_t' could not be resolved
Unknown type name 'mb32_packet_t'; use 'struct' keyword to refer to the type

typedef struct 是否恰好用于此目的,即在定义此类型的变量时能够省略struct关键字?

Isn't typedef struct intended for exactly this purpose, i.e. to be able to omit the struct keyword when defining variables of this type?

推荐答案

typedef 定义的别名称为 mb32_packet_s .因此,您需要将其用作

Your alias defined by typedef is called mb32_packet_s. So you need to use it as

mb32_packet_s packet;

struct mb32_packet_t packet;

您还可以将别名重命名为 mb32_packet_t :

You can also rename the alias to mb32_packet_t:

typedef struct mb32_packet_t {
  uint8_t compid;
  uint8_t servid;
  uint8_t payload[248];
  uint8_t checksum;
} __attribute__((packed)) mb32_packet_t;

然后您可以同时使用两种名称(原始名称不带别名)

Then you can do both (original name without alias)

struct mb32_packet_t packet;

和(带别名)

mb32_packet_t packet;

这样,别名和struct的名称是相同的,但是从技术上讲, struct mb32_packet_t mb32_packet_t 是两种不同的东西,但是它们引用的是同一类型.

This way, the names of both alias and the struct are identical, but technically, struct mb32_packet_t and mb32_packet_t are two different things that however refer to the same type.

这篇关于为什么无法解决此类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆