如何在 .h 中转发 typedef 结构 [英] How to forward typedef'd struct in .h
问题描述
我有预处理器.h
#define MAX_FILES 15
struct Preprocessor {
FILE fileVector[MAX_FILES];
int currentFile;
};
typedef struct Preprocessor Prepro;
void Prepro_init(Prepro* p) {
(*p).currentFile = 0;
}
那时我意识到我必须将声明与定义分开.所以我创建了 Preprocessor.c:
I realized then that I had to separate declarations from definitions. So I created Preprocessor.c:
#define MAX_FILES 15
struct Preprocessor {
FILE fileVector[MAX_FILES];
int currentFile;
};
typedef struct Preprocessor Prepro;
Preprocessor.h 现在是:
And Preprocessor.h is now:
void Prepro_init(Prepro* p) {
(*p).currentFile = 0;
}
这显然行不通,因为 Pr..h 不知道 Prepro 类型.我已经尝试了几种组合,但都没有奏效.我找不到解决方案.
That obviously, doesn't work because Pr..h doesn't know Prepro type. I already tried several combinations, none of them worked. I can't find the solution.
推荐答案
将 typedef struct Preprocessor Prepro;
移动到文件头和 c 文件中的定义以及 Prepro_init 定义.这将毫无问题地为您转发声明.
Move the typedef struct Preprocessor Prepro;
to the header the file and the definition in the c file along with the Prepro_init definition. This is will forward declare it for you with no issues.
预处理器.h
#ifndef _PREPROCESSOR_H_
#define _PREPROCESSOR_H_
#define MAX_FILES 15
typedef struct Preprocessor Prepro;
void Prepro_init(Prepro* p);
#endif
预处理器.c
#include "Preprocessor.h"
#include <stdio.h>
struct Preprocessor {
FILE fileVector[MAX_FILES];
int currentFile;
};
void Prepro_init(Prepro* p) {
(*p).currentFile = 0;
}
这篇关于如何在 .h 中转发 typedef 结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!