所以我正在学习操作系统课程,并且我们以 C 编程速成课程开始了这个学期。我们的前两项任务很简单,但我终其一生都无法弄清楚这一点。
所以我们的教授让他的助教创建了以下 .c 文件:
#include <stdio.h>
#include <stdlib.h>
#include "dll.h"
void init(Dll *dll, int n){
Node *prev = &(dll->head);
prev->prev = NULL;
Node *node;
for(int i=0;i<n;i++){
node = (Node *)malloc(sizeof(Node));
node->value = rand();
node->prev = prev;
node->next = NULL;
prev->next = node;
prev = node;
printf("%d. ", i+1);
printf("%d\n", node->value);
}
}
void print(Dll *dll){
Node *current;
current = dll->head.next;
while(current->next != NULL){
printf("%d\n", current->value);
current = current->next;
}
printf("%d\n", current->value);
}
void sort(Dll *dll){
int changed = 1;
while(changed==1){
Node *current;
current = dll->head.next;
changed = 0;
while(current->next != NULL){
if(current->value > current->next->value){
int value;
value = current->value;
current->value = current->next->value;
current->next->value = value;
changed = 1;
}
current = current->next;
}
}
}
int main(){
Dll dll;
init(&dll, 10);
print(&dll);
sort(&dll);
printf("\nSorted:\n");
print(&dll);
return 0;
}
我们的任务是创建头文件,我们应该实现它来运行 .c 文件。我花了一整天的时间在上面工作,我能想到的最好的方法是:
#include <stdio.h>
#include <stdlib.h>
typedef struct Dll {
Dll *prev;
Dll *next;
int value;
} dll;
void *init(Dll *dll, int n);
void sort(Dll *dll);
void print(Dll *dll);
每当我用我的 .h 运行它时,我都会在命令行中收到以下错误(我们在 linux 上使用 gcc):
In file included from dll.c:5:0:
dll.h:5:5: error: unknown type name ‘Dll’
dll.h:6:5: error: unknown type name ‘Dll’
dll.h:10:1: error: unknown type name ‘Dll’
dll.h:12:12: error: unknown type name ‘Dll’
dll.h:13:11: error: unknown type name ‘Dll’
dll.h:14:12: error: unknown type name ‘Dll’
dll.c:7:11: error: unknown type name ‘Dll’
dll.c:22:12: error: unknown type name ‘Dll’
dll.c:31:11: error: unknown type name ‘Dll’
dll.c: In function ‘main’:
dll.c:51:2: error: unknown type name ‘Dll’
我使用这些类型的头文件的其他示例来制作我的头文件,但由于某种原因,我无法弄清楚这一点。我真的很感激你们能给我的任何帮助。
*编辑:*当我将名称更改为“Dll”时,它给了我:
In file included from dll.c:5:0:
dll.h:5:5: error: unknown type name ‘Dll’
dll.h:6:5: error: unknown type name ‘Dll’
dll.h:10:1: error: unknown type name ‘Type’
dll.h:12:5: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘*’ token
dll.c:7:6: error: conflicting types for ‘init’
dll.h:14:7: note: previous declaration of ‘init’ was here
dll.c: In function ‘init’:
dll.c:8:2: error: unknown type name ‘Node’
dll.c:8:20: error: ‘Dll’ has no member named ‘head’
dll.c:9:6: error: request for member ‘prev’ in something not a structure or union
dll.c:10:2: error: unknown type name ‘Node’
dll.c:11:2: error: ‘for’ loop initial declarations are only allowed in C99 mode
dll.c:11:2: note: use option -std=c99 or -std=gnu99 to compile your code
dll.c:12:11: error: ‘Node’ undeclared (first use in this function)
dll.c:12:11: note: each undeclared identifier is reported only once for each function it appears in
dll.c:12:17: error: expected expression before ‘)’ token
dll.c:13:7: error: request for member ‘value’ in something not a structure or union
dll.c:14:7: error: request for member ‘prev’ in something not a structure or union
dll.c:15:7: error: request for member ‘next’ in something not a structure or union
dll.c:16:7: error: request for member ‘next’ in something not a structure or union
dll.c:19:22: error: request for member ‘value’ in something not a structure or union
dll.c: In function ‘print’:
dll.c:23:2: error: unknown type name ‘Node’
dll.c:24:15: error: ‘Dll’ has no member named ‘head’
dll.c:25:15: error: request for member ‘next’ in something not a structure or union
dll.c:26:25: error: request for member ‘value’ in something not a structure or union
dll.c:27:20: error: request for member ‘next’ in something not a structure or union
dll.c:29:24: error: request for member ‘value’ in something not a structure or union
dll.c: In function ‘sort’:
dll.c:34:3: error: unknown type name ‘Node’
dll.c:35:16: error: ‘Dll’ has no member named ‘head’
dll.c:37:16: error: request for member ‘next’ in something not a structure or union
dll.c:38:14: error: request for member ‘value’ in something not a structure or union
dll.c:38:31: error: request for member ‘next’ in something not a structure or union
dll.c:40:20: error: request for member ‘value’ in something not a structure or union
dll.c:41:12: error: request for member ‘value’ in something not a structure or union
dll.c:41:29: error: request for member ‘next’ in something not a structure or union
dll.c:42:12: error: request for member ‘next’ in something not a structure or union
dll.c:45:21: error: request for member ‘next’ in something not a structure or union