我向项目添加了一个新文件:
#ifndef PLAYER_H
#define PLAYER_H
#include "enet/enet.h" //the problem
typedef struct Player
{
ENetPeer * peer; //requires problematic include
//void * peer; //works, since no include required
} Player;
const struct Player playerEmpty;
#endif //PLAYER_H
如果 include
存在,我会在不相关的文件中得到大量的 error: expected ';', ',' or ')' before numeric constant
。如果我删除 include
并改用 void * peer
,一切都很好。 enet 库包含在其他地方的源文件中,并且工作正常。我使用的是 enet 1.3.13(最新版),它的头部防护装置似乎就位。这是在 gcc 4.9.2 下。
郑重声明错误发生在Point.h
中:
#ifndef POINT_H
#define POINT_H
#include <stdint.h>
#define X 0
#define Y 1
#define Z 2
typedef int16_t int16_Point2[2];
typedef int32_t int32_Point2[2];
typedef uint16_t uint16_Point2[2];
typedef uint32_t uint32_Point2[2];
typedef int16_t int16_Point3[3];
typedef int32_t int32_Point3[3];
typedef uint16_t uint16_Point3[3];
typedef uint32_t uint32_Point3[3];
#endif //POINT_H
我确定这很简单 - 知道我做错了什么吗?
最佳答案
一般来说,使用单字母宏名称是个好主意。它们可能很容易替换意想不到位置的字母(注意:宏实际上是实际编译阶段之前的文本替换)。
您写的错误发生在 Point.h 中。我不认为它们实际上发生,而只是在这里报道。众所周知,C 无法在实际位置检测到语法错误。检查包含Point.h的文件
注意: header 中的 const struct Player playerEmpty;
也可能是不需要的,因为这会在每个编译单元中创建一个具有外部链接的对象。这与 C++ 不同:在 C 中,实际上没有常量,只有 常量变量:const
只是程序员的 promise 变量一旦初始化就永远不会改变.更糟糕的是:你没有给它赋值,从而使它有效地 0
- 全局变量被初始化为所有位 0。我很确定这不是故意的。
更新:
如果那是为了积分,怎么样:
typedef union __attribute__ ((__packed__)) {
struct {
int16_t x,y,z;
}; // anonymous union field (C99)
int16_t vec[3];
} int16_Point3;
...
// usage:
int16_Point3 point = (int16_Point3){ .x = 5, .y = 3 }; // compound literal
point.z = point.x + point.vec[1]; // other word for point.y
摆脱 #define
并获得正确的语法。
注意 __attribute__ ((__packed__))
是特定于 gcc 的,以避免在结构字段之间填充字节。这是非标准的,但其他编译器通常具有类似的功能(例如 pragma
)。结构和数组的布局必须相同。
这可能比索引更具可读性。请注意,匿名结构和 union 字段是标准的。
关于c - GCC 报告包含的无关错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31089558/