所以我必须编写一些代码,用于创建一个列表并对其执行各种操作。具体地,打印它,排序它,并查看其中是否存在某个值。已完成,并能正常运行。现在我必须将这些函数拆分成单独的文件,然后使用gcc -c(我不确定自己是否使用正确)获取.o文件,以及测试程序的.o文件。然后,我必须使用gcc将我的.o文件链接成可执行文件。提示会识别.o文件并知道如何链接它们。
那么,这里是我的问题: 为什么以下代码会返回错误(错误方式将在下面定义)? 以及 我应该在命令行中写入什么来链接这些文件?
所以代码如下: (首先是.h文件,然后是主要的.c文件)
node.h
现在,当所有代码都在一起时,它们都可以很好地运行,除了node.h文件(它总是一个单独的文件)。.h文件将完美编译,但是当我尝试编译.c文件时,会返回错误,声称我正在尝试在每个.h文件中重新定义node。这可能是因为我在每个.h文件中都包含它吗?
此外,我还遇到了错误,声称我正在尝试将不适当的参数传递给init函数,但事实并非如此。但我可能只是忽略了一些东西。
提前感谢您的任何帮助。
编辑:在主函数中将变量从new更改为start。 在输入“gcc list.c”时出现错误。
(然后还有另一个用于第二个打印函数的)
那么,这里是我的问题: 为什么以下代码会返回错误(错误方式将在下面定义)? 以及 我应该在命令行中写入什么来链接这些文件?
所以代码如下: (首先是.h文件,然后是主要的.c文件)
node.h
typedef struct Node{
int data;
struct Node *next;
struct Node *prev;
}node;
print.h
#include<stdio.h>
#include"node.h"
void print(node *pointer){
if (pointer == NULL){
return;
}
printf("%d ",pointer->data);
print(pointer->next);
}
init.h
#include<stdio.h>
#include"node.h"
int init(node *pointer,int find){
pointer = pointer->next;
while (pointer != NULL){
if (pointer->data == find)//found find
{
printf("The data is in the list.");
return 1;
}
pointer = pointer->next;// Search in the next node.
}
//find is not found
printf("The data is not in the list.");
return 0;
}
sort.h
#include<stdio.h>
#include"node.h"
void swap (node *x, node *y){
int temp = x->data;
x->data = y->data;
y->data = temp;
}
void sort(node*pointer){
int i;
while (pointer->next != NULL){
if (pointer->data>pointer->next->data){
swap(pointer,pointer->next);
}
pointer = pointer->next;
sort(pointer);
}
}
list.c
#include<stdio.h>
#include<stdlib.h>
#include"node.h"
#lnclude"print.h"
#include"sort.h"
#include"init.h"
int i;
node *p;
node *n;
void insert(node *pointer, int data){
//go through list till ya find the last node
while (pointer->next != NULL){
pointer = pointer->next;
}
//allocate memory for new node and put data in it
pointer->next = (node *)malloc(sizeof(node));
(pointer->next)->prev = pointer;
pointer = pointer->next;
pointer->data = data;
pointer->next = NULL;
}
int main(){
//start is used to point to the first node
//temp is for the last node
node *start, *temp;
int z;
start = (node *)malloc(sizeof(node));
temp = new;
temp->next = NULL;
temp->prev = NULL;
for (z = 0; z < 10; z++){
insert(start,(3*10) - z);
}
init(start,12);
init(start,3);
init(start,27);
init(start,7);
print(start);
sort(start);
print(start);
}
现在,当所有代码都在一起时,它们都可以很好地运行,除了node.h文件(它总是一个单独的文件)。.h文件将完美编译,但是当我尝试编译.c文件时,会返回错误,声称我正在尝试在每个.h文件中重新定义node。这可能是因为我在每个.h文件中都包含它吗?
此外,我还遇到了错误,声称我正在尝试将不适当的参数传递给init函数,但事实并非如此。但我可能只是忽略了一些东西。
提前感谢您的任何帮助。
编辑:在主函数中将变量从new更改为start。 在输入“gcc list.c”时出现错误。
In file included from init.h:2:0,
from list.c:4:
node.h:1:16 error: redefinition of'struct Node'
node.h:1:16 note: originally defined here
node.h:5:2 error: conflicting types for 'node'
node.h:5:2 note: previous declaration of 'node' was here
In file included from sort.h:2:0;
from list.c:5:
node.h:1:16 error: redefinition of'struct Node'
node.h:1:16 note: originally defined here
node.h:5:2 error: conflicting types for 'node'
node.h:5:2 note: previous declaration of 'node' was here
list.c: In function 'main':
list.c:41:1: warning: passing argument 1 of 'init' from incompatible pointer type[enabled by default]
init.h:4:5: note: expected 'struct node *' but argument is of type 'struct node *'
接着,主函数中的每个 init 调用都会出现错误。
list.c:45:1: warning:passing argument 1 of 'print' from incompatible pointer type [enabled by default]
print.h:3:6: note expected 'struct node *' but argument is of type 'struct node *'
(然后还有另一个用于第二个打印函数的)
new
的建议。C和C++是不同的语言,阅读或编写C代码不应该需要了解C++。我个人感觉没有必要避免使用new
(一个C++关键字)或者subtract
(一个Cobol关键字)来编写C代码。此外,在C代码中使用new
作为标识符可以避免将其错误地编译为C++的错误。(当然,如果你确实有编写既可作为C又可作为C++编译的实际需求,那就是另一回事了。) - Keith Thompson