变量声明为从未完成的类型

Variable was declare with a never-completed type

我有三个文件

我阅读了一些post关于在哪里定义结构和封装的内容,我想在头文件中声明我的结构并在源文件中定义它。

这是我测试过的。

myStruct.h

// myStruct.h
#include "stdint.h"

typedef struct myStruct myStruct_type;

myStruct.c

// myStruct.c
#include "myStruct.h"

struct myStruct {
    uint32_t itemA;
    uint32_t itemB;
    uint32_t *pointerA;
    uint32_t *pointerB;
};

main.c

// main.c
#include "myStruct.h"

myStruct_type testStruct;    // This is where I get the error message

int main (void) {
    while (1);

    return 0;
}

当我尝试编译 (Keil uVision) 时出现以下错误 "Variable 'testStruct' was declare with a never-completed type myStruct_type testStruct"

我错过了什么?

你不能这样声明 testStruct,myStruct_type 是一个不完整的类型。您最多可以声明一个指针。

所以改变

myStruct_type testStruct;

myStruct_type *testStruct;

你可以这样想,当编译器在编译main.c时,它没有myStruct_type中成员的信息,所以它无法计算结构的大小。