void xiugai(node *h)中为啥有edefintion of "xiugai"的错误
时间: 2024-03-28 15:38:02 浏览: 18
这个错误是因为在程序中出现了"xiugai"的重复定义。可能是在同一个源文件中定义了多个名为"xiugai"的函数,或者在不同的源文件中都包含了"xiugai"函数的定义。
为了避免这个错误,可以采取以下措施:
1. 检查程序中是否有同名的函数或变量。如果有,需要修改名称以避免重复定义。
2. 确保每个函数的定义只出现在一个源文件中。如果需要在多个源文件中使用某个函数,可以将函数的声明放在头文件中,然后在需要使用该函数的源文件中包含头文件即可。
3. 如果使用了多个源文件编译成一个可执行文件,需要在链接时指定正确的编译选项,以确保不会出现重复定义的错误。
相关问题
node *modification(node *h){//修改订单 node *s,*r; if(h==NULL) { h=malloc(sizeof(node)); h->next=NULL; r=h; } else{ r=h; while(r->next!=NULL) r=r->next; } s=malloc(sizeof(node)); printf("名称:"); scanf("%s",s->data.name); printf("增加时间:"); scanf("%lf",&s->data.time); printf("类型:"); scanf("%d",&s->data.type); r->next=s; r=s; r->next=NULL; return h; } void xiugai(node *s){//修改订单 while(s->next!=NULL){ s=s->next; printf("名称:"); printf("%s\n",&s->data.name); printf("增加时间:"); printf("%s\n",&s->data.time); printf("手机尾号:"); printf("%s\n",&s->data.useful); printf("类型:"); printf("%s\n",&s->data.type); } }这段代码插入上一段中是否会存在问题
这段代码存在一些问题。具体问题如下:
1. 函数`modification`的定义中,应该在参数列表中指明参数的类型,例如`node *modification(node *h)`应该改为`node* modification(node* h)`,这样更符合C语言的函数定义规范。
2. 函数`modification`中,如果链表`h`为空,则应该先为其分配一个头结点,然后将新节点插入到头结点之后,代码中已经实现了这个逻辑。
3. 函数`modification`中,将新节点插入到链表的末尾,代码中已经实现了这个逻辑。
4. 函数`xiugai`中,应该将链表头节点的数据忽略掉,否则第一个节点的数据将没有被输出。
5. 函数`xiugai`中,输出节点数据时,应该使用`s->data.name`,`s->data.time`等成员变量,而不是`&s->data.name`,`&s->data.time`等成员变量的地址。
6. 函数`xiugai`中,输出节点数据的格式应该和输入时一致,例如名称应该用`%s`而不是`%lf`。
7. 函数`xiugai`中,输出节点数据时,应该先输出提示信息,再等待用户输入数据,代码中已经实现了这个逻辑。
综上所述,修改后的代码如下:
```
node* modification(node* h) {
node *s, *r;
if (h == NULL) {
h = malloc(sizeof(node));
h->next = NULL;
r = h;
} else {
r = h;
while (r->next != NULL)
r = r->next;
}
s = malloc(sizeof(node));
printf("名称:");
scanf("%s", s->data.name);
printf("增加时间:");
scanf("%lf", &s->data.time);
printf("类型:");
scanf("%d", &s->data.type);
r->next = s;
r = s;
r->next = NULL;
return h;
}
void xiugai(node* s) {
printf("修改订单:\n");
while (s->next != NULL) {
s = s->next;
printf("名称:%s\n", s->data.name);
printf("增加时间:%lf\n", s->data.time);
printf("类型:%d\n", s->data.type);
}
}
```
void xiugai(node *s){ int ch; system("cls"); system("color 6B"); printf("\n\n\n\t~~~~~~~修改订单~~~~~~~~~\n\n"); printf("\t输入1修改订单\t输入0返回上一级:"); scanf("%d",&ch); while(1){ switch(ch){ case 1:printf(h);break; case 0:printf("0");break; default:online();//退出 } printf("\t输入1修改订单\t输入0返回上一级:"); scanf("%d",&ch); if(ch==0){ printf("返回上一级\n"); break; } } }node* modification(node *h){//修改订单 node *s,*r; if(h==NULL) { h=malloc(sizeof(node)); h->next=NULL; r=h; } else{ r=h; while(r->next!=NULL) r=r->next; } s=malloc(sizeof(node)); printf("名称:"); scanf("%s",s->data.name); printf("增加时间:"); scanf("%lf",&s->data.time); printf("类型:"); scanf("%d",&s->data.type); r->next=s; r=s; r->next=NULL; return h; } void xiugai(node *s){ printf("修改订单:\n");//修改订单 while(s->next!=NULL){ s=s->next; printf("名称:"); printf("%s\n",&s->data.name); printf("增加时间:"); printf("%s\n",&s->data.time); printf("手机尾号:"); printf("%s\n",&s->data.useful); printf("类型:"); printf("%s\n",&s->data.type); } }
这段代码中存在以下问题:
1. `void xiugai(node *s)` 函数被定义了两次,这会导致编译错误。需要将其中一个函数的名称修改为其他名称。
2. `case 1:printf(h);` 应该改为 `case 1:xiugai(h);`,因为需要调用 `xiugai` 函数来修改订单。
3. `scanf("%s",&s->data.time);` 应该改为 `scanf("%lf",&s->data.time);`,因为订单时间是一个实数类型。
4. `printf("%s\n",&s->data.time);` 应该改为 `printf("%lf\n",s->data.time);`,因为订单时间是一个实数类型,需要使用 `%lf` 格式化符号输出。
5. `printf("%s\n",&s->data.useful);` 应该改为 `printf("%d\n", s->data.useful);`,因为手机尾号是一个整数类型。
下面是修改后的代码:
```c
void modify(node *s) {
int ch;
system("cls");
system("color 6B");
printf("\n\n\n\t~~~~~~~修改订单~~~~~~~~~\n\n");
printf("\t输入1修改订单\t输入0返回上一级:");
scanf("%d", &ch);
while (1) {
switch (ch) {
case 1:
xiugai(s);
break;
case 0:
printf("返回上一级\n");
break;
default:
online(); //退出
break;
}
printf("\t输入1修改订单\t输入0返回上一级:");
scanf("%d", &ch);
if (ch == 0) {
printf("返回上一级\n");
break;
}
}
}
node* xiugai(node *h) {
node *s, *r;
if (h == NULL) {
h = malloc(sizeof(node));
h->next = NULL;
r = h;
} else {
r = h;
while (r->next != NULL)
r = r->next;
}
s = malloc(sizeof(node));
printf("名称:");
scanf("%s", s->data.name);
printf("增加时间:");
scanf("%lf", &s->data.time);
printf("手机尾号:");
scanf("%d", &s->data.useful);
printf("类型:");
scanf("%d", &s->data.type);
r->next = s;
r = s;
r->next = NULL;
return h;
}
void print(node *s) {
printf("修改订单:\n");
while (s->next != NULL) {
s = s->next;
printf("名称:%s\n", s->data.name);
printf("增加时间:%lf\n", s->data.time);
printf("手机尾号:%d\n", s->data.useful);
printf("类型:%d\n", s->data.type);
}
}
```