我收到这样的错误:

$ 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代码是我一生的 Backbone 。

附言:我已经检查了所有的#include“lliSTLib.h”,它们都包含在lliSTLib.c文件和llistdriver.c文件中。

好吧,这可能会使它更清晰

//lliSTLib.c-实现liSTLib.h文件中定义的功能的库
#包括
#包括
#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);

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

最佳答案

您的finish函数缺少右括号{。 search有一个额外的大括号。

关于c - 为什么我在执行单链列表时在C中收到 undefined reference 错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13984765/

10-09 08:44