"["令牌和 + 之前应为非限定 ID

expected unqualified-id before '[' token and +

本文关键字:ID 令牌      更新时间:2023-10-16

我从未在Arduino Nano(Atmega328)上遇到过一个未知问题的问题。我正在构建无人机源代码,而且状况良好。但是,Arduino Ide突然出现了错误。
显然,我是编程的新秀。因此,请帮助我找出如何解决这个问题。

我有这样的错误:

error: expected unqualified-id before '[' token 
error: 'value' does not name a type
 Quadcopter_Code_Arduino:580: error: expected unqualified-id 
 before '[' token
  struct op[]
           ^
Quadcopter_Code_Arduino:589: error: 'value' does not name a type
   } value;
     ^
typedef union accel_t_gyro_union
{
  struct
  {
    uint8_t x_accel_h;
    uint8_t x_accel_l;
    uint8_t y_accel_h;
    uint8_t y_accel_l;
    uint8_t z_accel_h;
    uint8_t z_accel_l;
    uint8_t t_h;
    uint8_t t_l;
    uint8_t x_gyro_h;
    uint8_t x_gyro_l;
    uint8_t y_gyro_h;
    uint8_t y_gyro_l;
    uint8_t z_gyro_h;
    uint8_t z_gyro_l;
  } reg;
  struct op[]
  {
    int x_accel;
    int y_accel;
    int z_accel;
    int temperature;
    int x_gyro;
    int y_gyro;
    int z_gyro;
  } value;
};

struct op[] { ... }对我来说很奇怪。(我什至不知道如何阅读/解释。)

因此,我在Cygwin上使用GCC(在简化版本中)进行了测试:

$ gcc --version
gcc (GCC) 5.4.0
$ echo 'typedef union u { struct op[] { int i; } value; };
> int main() { return 0; } 
> ' > test-weird-union.c
$ gcc test-weird-union.c   
test-weird-union.c:1:28: error: expected identifier or '(' before '[' token
 typedef union u { struct op[] { int i; } value; };
                            ^
test-weird-union.c:1:42: error: expected ';' before 'value'
 typedef union u { struct op[] { int i; } value; };
                                          ^
test-weird-union.c:1:42: warning: useless storage class specifier in empty declaration

(与gcc -std=c11相同的输出。)

代码可能是错误的,或者至少不是标准c。

好的。另外考虑:如何修复它?

更多背景会很好。因此,将其视为一种方法并证明如何应用它:

#include <stdint.h>
typedef union accel_t_gyro_union
{
  struct
  {
    uint8_t x_accel_h;
    uint8_t x_accel_l;
    uint8_t y_accel_h;
    uint8_t y_accel_l;
    uint8_t z_accel_h;
    uint8_t z_accel_l;
    uint8_t t_h;
    uint8_t t_l;
    uint8_t x_gyro_h;
    uint8_t x_gyro_l;
    uint8_t y_gyro_h;
    uint8_t y_gyro_l;
    uint8_t z_gyro_h;
    uint8_t z_gyro_l;
  } reg;
  struct op
  {
    int16_t x_accel;
    int16_t y_accel;
    int16_t z_accel;
    int16_t temperature;
    int16_t x_gyro;
    int16_t y_gyro;
    int16_t z_gyro;
  } value;
} accel_t_gyro_union;

我做过的以下事情:

  1. 删除了op后面的[]以修复语法错误。

  2. int更改为int16_t。这确保此代码在非16位平台上也有效。

  3. accel_t_gyro_union添加到typedef的末尾。这可能不是必需的,但我对此感到满意。

再次与GCC在Cygwin上检查:

$ gcc -std=c11 test-weird-union.c

至少没有语法错误。剩下的超出了我的知识。

定义/信息:

来自http://en.cppreference.com/w/cpp/language/union

联合是一种特殊的类型,只能容纳其中一种 一次非静态数据成员。

持有其最大数据成员的工会只有必要的大小。 其他数据成员分配给了与此相同的字节 最大成员。

也:

您的声明不正确,应该是:

struct op
{
    int x_accel;
    int y_accel;
    int z_accel;
    int temperature;
    int x_gyro;
    int y_gyro;
    int z_gyro;
} value [SIZE];

我建议您为您的数组使用特定尺寸,或者对结构类型的指针拆分来清除,您将稍后确定其堆尺寸,但是堆栈上的联合将具有指针尺寸成员...

(尽管使用空括号,它仍然在我的快速测试中进行编译和工作...我也应该阅读此行为。:D)

解决方案:

使用上面更正的声明。

建议:使用括号中的数组使用大小。如果您还不知道尺寸,请出于上述原因使用指针。

struct op[] { ... }无论如何都是胡说八道。如果您想要名为opstruct类型,它应该不在联合之外(但这不是真正的问题,可能会有一个问题要在其他地方使用):

struct A {
    uint8_t  low_byte; // depends on byte order 
    uint8_t high_byte;
};
struct B {
    int          word;
};
union U {
    A  reg;
    B  val;
} variable;

,但根本不需要阵列。因此,第二个struct也应该是匿名的:

union accel_t_gyro_union
{
  struct
  {
    uint8_t x_accel_h;
    uint8_t x_accel_l;
    uint8_t y_accel_h;
    uint8_t y_accel_l;
    uint8_t z_accel_h;
    uint8_t z_accel_l;
    uint8_t t_h;
    uint8_t t_l;
    uint8_t x_gyro_h;
    uint8_t x_gyro_l;
    uint8_t y_gyro_h;
    uint8_t y_gyro_l;
    uint8_t z_gyro_h;
    uint8_t z_gyro_l;
  } reg;
  struct
  {
    int x_accel;
    int y_accel;
    int z_accel;
    int temperature;
    int x_gyro;
    int y_gyro;
    int z_gyro;
  } value;
};