我有以下文件结构,其中包含封装类型结构的定义,当我尝试访问结构的成员时,Member access into incomplete type出现错误。问题是什么?
foo_encoder.c :
#include "foo.h"
//...
struct FooEncoder {
int A;
int B;
foo_int32 C;
//...
}
Run Code Online (Sandbox Code Playgroud)
foo.h :
extern "C" {
typedef struct FooEncoder FooEncoder;
//...
}
Run Code Online (Sandbox Code Playgroud)
foo_interface.h :
typedef struct MyFooEncInst FooEncInst;
Run Code Online (Sandbox Code Playgroud)
foo_interface.cc :
#include "foo_interface.h"
#include "foo.h"
//...
struct MyFooEncInst {
FooEncoder* encoder;
};
//...
MyFoo_Encode(FooEncInst* inst,...) {
//...
if (d > inst->encoder->C) { // This is where I get the error
//...
}
Run Code Online (Sandbox Code Playgroud)
foo_int32 在另一个地方定义。
您要求FooEncoder结构中的成员在foo_interface.cc文件中的任何位置都不可见。这看起来类似于pimpl 成语。
为了让您的代码了解FooEncoder的结构,您需要
#include "foo_encoder.c"
Run Code Online (Sandbox Code Playgroud)
在你的foo_interface.cc文件中(我非常不喜欢这个解决方案,你也没有发布完整的代码)或者将你的结构定义移动到头文件中的其他地方并包含那个(推荐)。