不完整类型定义后的 Typedef

Саш*_*лов 0 c++ struct typedef compiler-errors redeclaration

我需要siginfo_t在标头中声明不完整的类型(可能是),并在源导入中sys/signal.h定义该类型的一些系统标头()。

所以,问题来了:

// a.h
struct siginfo_t;
void foo(siginfo_t*);
Run Code Online (Sandbox Code Playgroud)
// a.cpp
#include "a.h"
#include <signal.h> 
void foo(siginfo_t* x) { /* implementation */ };
Run Code Online (Sandbox Code Playgroud)

但在 signal.h 中它的定义如下:

typedef struct siginfo_t { /* */ } siginfo_t;
Run Code Online (Sandbox Code Playgroud)

编译器错误:error: typedef redefinition with different types ('struct siginfo_t' vs 'siginfo_t').

如何实现没有错误呢?谢谢!

Vla*_*cow 5

在标头中,<sys/siginfo.h>typedef 名称siginfo_t是未命名结构的别名

typedef struct {
    int si_signo;
    int si_code;
    union sigval si_value;
    int si_errno;
    pid_t si_pid;
    uid_t si_uid;
    void *si_addr;
    int si_status;
    int si_band;
} siginfo_t;
Run Code Online (Sandbox Code Playgroud)

但是您为命名结构引入了相同的别名

typedef struct siginfo_t { /* */ } siginfo_t;
Run Code Online (Sandbox Code Playgroud)

所以编译器会发出错误

错误:使用不同类型的 typedef 重新定义(“struct siginfo_t”与“siginfo_t”)

因为您不能为命名结构和未命名结构引入相同的别名。这些结构是不同的类型。