0

我收到这样的错误

$ gcc -o app llistdriver.c llistlib.c -Wall
 llistdriver.c:7: warning: return type defaults to `int'
 llistdriver.c: In function `printinsts':
 llistdriver.c:15: warning: control reaches end of non-void function
 llistlib.c: In function `insert':
 llistlib.c:25: warning: statement with no effect
 llistlib.c:41: warning: control reaches end of non-void function
 /tmp/ccqYmK3M.o:llistdriver.c:(.text+0x157): undefined reference to `_search'
 collect2: ld returned 1 exit status

而且我无法弄清楚为什么我收到此错误消息已经编写了一个包含链接列表方法的库文件。

  int search( struct node *front, int val)// 
 {
     while(front != NULL)
    {
       if( front->modmark == val ) //this is to find the value 
       return 1;  // found it

    front=front->next;
    }
    return 0;  // Not found
  }

我认为它抱怨主文件中的唯一引用

        case 5: 
                printf("please enter a search value?\n");
                scanf("%i",&mark);
                choice = search(first,mark);//here
                if (choice == -1)
                {

                    printf("the modmark was not found\n");
                }
                else 
                {   
                    printf("the first occurence of the mark is at the %i th index of the list",choice); 
                };
                    break;

并且 first 和 mark 是这样定义的变量

                 struct node *first;
              int mark = 0;

对此的任何帮助将不胜感激我花了几个小时试图调试这个错误。编写 C 代码是我目前生活的祸根。

Ps 我已经检查了 llistlib.c 文件和 llistdriver.c 文件中包含的所有#include "llistlib.h"。

好吧,也许这会让它更清楚

// llistlib.c - 实现 listlib.h 文件中定义的函数的库 #include #include #include "llistlib.h"

void initialise(struct node **ps)
{

printf("initialise function\n");

*ps = NULL;   //initialise list to empty

}


int insert(struct node **fn)
{
printf("insert function\n");
struct node *newnode;
newnode =  (struct node*) malloc(sizeof (struct node)); //make new node and give it memory
if (newnode == NULL) //if fsiled return to the program saying it failed
    return -1;
if (*fn == NULL) // for the first of the list
{
    newnode -> next == NULL; // sets the next value to nothing
    printf("what mark do you want to enter\n");
    scanf("%i",&newnode -> modmark); // this gets the modmark
    *fn = newnode; //the head of the list is set to the new node
}
else
{
    newnode -> next = *fn; // sets the newnodes next value to the head node
    printf("what mark do you want to enter\n");
    scanf("%i",&newnode -> modmark); // gets the modmark
    *fn = newnode; // the head becomes the new node


    printf("%p %i",newnode,newnode->modmark); // just for debugging

}
 }



void traverse(struct node *ps)
{   

if (ps != NULL) // if the current node is null 
{
    printf("%p - %i\n",ps,ps -> modmark);
    traverse(ps -> next); 

}
else
{
    printf("your list is empty\n");
}
}


void delete(struct node **dn)
{
printf("delete function\n");
struct node *delnode;
delnode = *dn; //sets the head node so that it can be deleted later
if (delnode != NULL)
{
    *dn = delnode -> next; // sets the head to the next node
    free(delnode); //deletes the previous head node
}

}


void finish(struct node **ps) //this function goes through the list and removes all items
{
printf("finish function\n");
struct node *finishnode;
finishnode = *ps;
while (finishnode != NULL)
{
    *ps = finishnode -> next;
    free(finishnode);
    finishnode = *ps;
}

int search(struct node *front,int val)
{
 while(front != NULL)
 {
    if( front->modmark == val ) // Assuming struct node has val member which you are trying to compare to
       return 1;  // found it

    front=front->next;
 }
  return 0;  // Not found
}

}

然后 llistdriver.c 文件是

#include <stdio.h>
#include <stdlib.h>
#include "llistlib.h"


printinsts()
{
printf("Enter:- \n");
printf("\t0 to exit\n");
printf("\t1 to initialise list\n");
printf("\t2 to insert item at front of list\n");
printf("\t3 to delete item from front of list\n");
printf("\t4 to traverse list\n");
printf("\t5 to search through the list\n");
}




int main()
{
int choice;
struct node *first;
int mark = 0;

printinsts();
scanf("%d",&choice);

while (1)
{
    switch (choice)
    {
        case 0 : finish(&first);
                    exit(0);
                    break;

        case 1 : initialise(&first);
                    break;

        case 2 : if (insert(&first)== -1)
                    {
                        printf("insert not successful");
                        choice = 0;
                     };
                    break;

        case 3 : delete(&first);
                    break;

        case 4 : traverse(first);
                     break;
        case 5: 
                printf("please enter a search value?\n");
                scanf("%i",&mark);
                choice = search(first,mark);
                if (choice == -1)
                {

                    printf("the modmark was not found\n");
                }
                else 
                {   
                    printf("the first occurence of the mark is at the %i th index of the list",choice); 
                };
                    break;

        default : printf("Invalid choice of %d \n", choice);

    };  // end of switch

    printinsts();
    scanf("%d", &choice);

}  // end of while

}

头文件 llistlib.h 读取为

struct node {
int     modmark;
struct node *next;
 };


void initialise(struct node **ps);


int insert(struct node **fn);


void traverse(struct node *ps);


void delete(struct node **dn);


void finish(struct node **ps);


int search(struct node *front,int val);

我还没有在任何代码中发现明显的问题

4

4 回答 4

1

只需llistlib.h在您的llistlib.c文件中包含以下内容:

#include "llistlib.h"

并使用以下命令进行编译:

gcc -o app llistdriver.c llistlib.c

希望这可以帮助...

于 2012-12-21T06:43:07.907 回答
1

第一个错误:

 llistdriver.c:7: warning: return type defaults to `int'
 llistdriver.c: In function `printinsts':
 llistdriver.c:15: warning: control reaches end of non-void function

该函数如下所示:

printinsts() {}

请给这个函数一个返回类型,如果它不是 void,则返回它。

第二:

llistlib.c: In function `insert':
llistlib.c:25: warning: statement with no effect

那条线是:

newnode -> next == NULL; // sets the next value to nothing

注释不正确,此行没有设置任何内容,因为它使用相等运算符,而不是赋值运算符。

(这部分是品味问题,但我不喜欢这样的注释,它们只是试图说明与代码本身相同的东西。在这样的情况下,它们会变得误导,正如您可能阅读并相信的那样,注释,而不是在查找错误时解析代码。)

然后:

llistlib.c:41: warning: control reaches end of non-void function

这又是指:

int insert(struct node **fn)
{
printf("insert function\n");
struct node *newnode;
newnode =  (struct node*) malloc(sizeof (struct node)); //make new node and give it memory
if (newnode == NULL) //if fsiled return to the program saying it failed
    return -1;
...
}

除了 -1 之外,这个函数没有返回值。无论发生什么,它都应该返回一些东西。

最后:

/tmp/ccqYmK3M.o:llistdriver.c:(.text+0x157): undefined reference to `_search'

因为你从finish()的末尾错过了一个右大括号;

不要仅仅因为您的代码编译就忽略警告。仅仅修复错误是不够的!

于 2012-12-21T09:16:08.410 回答
0

您正在使用默认的 GCC 设置进行编译,即“大致遵循 C99 标准,然后添加非标准 GNU goo”。C99 标准以及当前的 C 标准不允许函数具有默认类型。这就是您使用 -Wall 收到警告的原因,返回类型必须是显式的。

printinsts() {}是无效的 C。您应该将其声明/定义为void printinsts (void),仅此而已。

将来,请确保始终使用 -std=c99 或 -std=c11 进行编译,以确保您将代码编译为标准 C 语言而不是其他语言。

于 2012-12-21T07:42:50.353 回答
0

您的finish函数缺少右大括号 {。并且search有一个额外的闭合支架。

于 2012-12-21T07:46:11.283 回答