如何在标头中没有完整结构定义的情况下定义和使用结构?

Ami*_*yan 0 c struct c89 design-guidelines

为了控制结构成员并强制程序员使用 getter/setter 函数,我想编写如下模式的代码:

/* Header file: point.h */
...
/* define a struct without full struct definition. */
struct point;

/* getter/setter functions. */
int point_get_x(const struct point* pt);
void point_set_x(struct point* pt, int x);
...

//--------------------------------------------

/* Source file: point.c */
struct point
{
  int x, y;
};

int point_get_x(const struct point* pt) {return pt->x; }

void point_set_x(struct point* pt, int x) {pt->x = x;}

//--------------------------------------------

/* Any source file: eg. main.c */

#include "point.h"
int main()
{
  struct point pt;

  // Good: cannot access struct members directly.
  // He/She should use getter/setter functions.
  //pt.x = 0;

  point_set_x(&pt, 0);
}
Run Code Online (Sandbox Code Playgroud)

但此代码无法使用 MSVC++ 2010 进行编译。

我应该对编译进行哪些更改?

注意:我使用 ANSI-C (C89) 标准,而不是 C99 或 C++。

Dou*_*rie 5

在point.c中创建一个make_point函数来创建点;main.c 不知道该结构有多大。

typedef struct point point;
Run Code Online (Sandbox Code Playgroud)

将支持使用point而不是struct point在声明中。