c 头文件和具有结构和类型定义的 .c 文件

c headers and .c files with structures and typedefs

我和往常一样过得很愉快。这次它与头文件、结构和我在某些 main.c 中使用的 .c 文件有关。我已经尝试了所有有意义的排列,但我无法得到合适的 link。这只是我尝试过的排列:

头文件:

struct MyHashMap;
struct node;
typedef struct MyHashMap MyHashMap;
typedef struct node node;
struct MyHashMap* myHashMapCreate();
void myHashMapPut(MyHashMap* obj, int key, int value);
int myHashMapGet(MyHashMap* obj, int key);
void myHashMapRemove(MyHashMap* obj, int key);
void myHashMapFree(MyHashMap* obj)

HashMap.c:

#include "HashMap.h"
struct MyHashMap;
struct node;

typedef struct MyHashMap MyHashMap;
typedef struct node node;

struct node{
    int key;
    int val;
    node* next;
};

struct MyHashMap{
    node** hashTable;
};

MyHashMap* myHashMapCreate() {
}

void myHashMapPut(MyHashMap* obj, int key, int value) {
...    
}

int myHashMapGet(MyHashMap* obj, int key) {
...
}

void myHashMapRemove(MyHashMap* obj, int key) {
...
}

void myHashMapFree(MyHashMap* obj) {
...
}

和主文件,some_application.c

#include "HashMap.h"

int main()
{
    MyHashMap * obj = myHashMapCreate();
}

这些 typedef 一直在践踏我的 include。并为我创建错误,例如:

HashMap.c: In function ‘myHashMapFree’:
HashMap.c:8:1: warning: empty declaration
    8 | struct MyHashMap;
      | ^~~~~~
HashMap.c:9:1: warning: empty declaration
    9 | struct node;
      | ^~~~~~
HashMap.c:11:26: error: storage class specified for parameter ‘MyHashMap’
   11 | typedef struct MyHashMap MyHashMap;

这一切对我来说都很有意义,但我和编译器绝对不是朋友。我不知道我们是否会成为朋友。就像壁球场上的那个女孩,我不打算停下来,直到发生好事或坏事。

哦,我尝试编译:

gcc -o some_application some_application.c HashMap.c

正如 Jonathan 在评论中提到的:

typedef struct MyHashMap MyHashMap;
typedef struct node node;
struct MyHashMap* myHashMapCreate(void);
void myHashMapPut(MyHashMap* obj, int key, int value);
int myHashMapGet(MyHashMap* obj, int key);
void myHashMapRemove(MyHashMap* obj, int key);
void myHashMapFree(MyHashMap* obj);

够了...