Visual C ++ 6.0中`typedef struct`的奇怪错误

时间:2011-06-29 16:41:38

标签: c visual-studio visual-c++ struct typedef

我正在尝试在MSVC 6.0中编译具有以下内容的纯C程序:

typedef struct _unitID {
    int sock;           // socket
    unsigned long au;    // the AU ID
} unitID;

然而,编译器引发了一个奇怪的错误:

error C 2085: 'unitID' not in formal parameter list

我不确定我是否理解编译器所抱怨的内容,因为根据microsoft's own page on the error这意味着“标识符在函数定义中声明,但不在正式参数列表中声明。”< / em>

考虑到上面的代码段构成了整个标题,并且文件中没有函数,如何才能成功编译?

[编辑 - 已解决]:由于此前包含文件中声明的typedef出现级联错误。

1 个答案:

答案 0 :(得分:0)

问题:

问题是由包含文件中声明的typedef引起的:

文件[main.c]:

#include <glib.h>
//...
#if (!GLIB_CHECK_VERSION(2,11,0))
typedef pid_t GPid;
#endif
//...
#include "someheader.h"
//...

文件[someheader.h]:

#include <stdio.h>
#include "unitid.h"
//...

文件[unitid.h]:

typedef struct _unitID {
    int sock;           // socket
    unsigned long au;    // the AU ID
} unitID;

第一个typedef的错误一直级联到unitid.h,导致奇怪的行为。因为我们使用旧版本的Glib构建,所以typedef会导致错误。

解决方案

解决方案是添加一个平台检查来处理两个平台如何处理PID的差异,如[main.c]中所示:

//...
#if (!GLIB_CHECK_VERSION(2,11,0))
#ifdef WIN32
  typedef void* GPid;
#else
  typedef int GPId;
#endif
#endif
//...