封獣鵺 2022-02-03 15:25 采纳率: 100%
浏览 59
已结题

[Error] ld returned 1 exit status

问题遇到的现象和发生背景

[Error] ld returned 1 exit status

问题相关代码,请勿粘贴截图
#include<stdio.h>

typedef struct QUOTE
{
    char speaker[128];
    char content[1024];
    struct QUOTE* next;
} quote;

void Quote_add();
void Quote_delete();
void Quote_all_show();
void Quote_modify();
void Speaker_search();
void quit();

void quit(quote* quoteCurrent) //退出
{
    quote* quiting;
    while (quoteCurrent != NULL)
    {
        *quiting = *(quoteCurrent->next);
        free(quoteCurrent);
        quoteCurrent = quiting;
    }
} 

void Quote_add(quote* quoteCurrent) //添加语录 
{
    extern numberCommand;
    quote* new = (quote *)malloc(sizeof(quote));
    if (new == NULL)
    {
        printf("你他妈中头奖了,内存分配失败了!");
    }
    printf("请输入语录内容:");
    scanf("%s",quoteCurrent->content);
    printf("请输入语录来源:");
    scanf("%s",quoteCurrent->speaker);
    if (quoteCurrent == NULL)
    {
        quoteCurrent = new;
    }
    else
    {
        quoteCurrent->next = new;
        quoteCurrent = new;
    }
    numberCommand = 0;
}
 
 void Quote_all_show(quote* quoteCurrent) //展示所有语录
{
    extern numberCommand;
     quote* quoteShowing = quoteCurrent;
     int numberQuote = 0;
     while (quoteShowing != NULL);
     {
     printf("%d.\n%s\n——%s\n\n",numberQuote++,quoteShowing->content,quoteShowing->speaker);
     quoteShowing = quoteShowing->next;
    }
    numberCommand = 0;
} 
 
void Quote_delete(quote* quoteCurrent) //删除语录
{
    extern numberCommand;
    Quote_all_show(quoteCurrent);
    printf("要删除第几个?");
    int numberQuoteTargetDelete = 0;
    quote* quoteTargetDelete = quoteCurrent;
    scanf("%d",&numberQuoteTargetDelete);
    int i = 0;
    for(i=0;i<numberQuoteTargetDelete-1;i++);
    {
        quoteTargetDelete = quoteTargetDelete->next;
    }
    free(quoteTargetDelete->next);
    quoteTargetDelete->next=quoteTargetDelete->next->next;
    numberCommand = 0;
} 

void Quote_modify(quote* quoteCurrent) //修改语录 
{
    extern numberCommand;
    Quote_all_show(quoteCurrent);
    printf("要修改第几个?");
    int numberQuoteModify = 0;
    scanf("%d",&numberQuoteModify);
    int j = 0;
    quote* quoteTargetModify = quoteCurrent;
    for(j=0;j<numberQuoteModify;j++)
    {
        quoteTargetModify=quoteTargetModify->next;
    }
    printf("修改人物【7】还是语句【8】?");
    scanf("%d",&numberCommand);
    if (numberCommand == 7)
    {
        printf("请输入人物:");
        scanf("%s",quoteTargetModify->speaker);
    }
    if (numberCommand == 8)
    {
        printf("请输入语录内容:");
        scanf("%s",quoteTargetModify->content); 
    }
    numberCommand = 0;
}

void Speaker_search(quote* quoteCurrent) //展示指定人物的所有语录
{
    extern numberCommand;
    printf("请输入人物名称:");
    char speakerTarget[128];
    scanf("%s",&speakerTarget);
    quote* quoteShowing = quoteCurrent;
     int numberQuote = 0;
     while (quoteShowing != NULL);
     {
         if (quoteShowing->speaker == speakerTarget)
         {
             printf("%d.\n%s\n\n",numberQuote++,quoteShowing->content);
        }
     quoteShowing = quoteShowing->next;
    }
    numberCommand = 0;
} 

int main(void)
{
    int numberCommand = 0;
    quote* quoteCurrent = NULL;
    while (numberCommand == 0)
    {
        printf("你要干嘛?\n【1】添加语录\n【2】删除语录\n【3】查看所有语录\n【4】修改语录\n【5】查找人物\n【6】退出\n");
        scanf("%d", &numberCommand);
        if (numberCommand == 1)
        {
            Quote_add(quoteCurrent);
        }
        if (numberCommand == 2)
        {
            Quote_delete(quoteCurrent);
        }
        if (numberCommand == 3)
        {
            Quote_all_show(quoteCurrent);
        } 
        if (numberCommand == 4)
        {
            Quote_modify(quoteCurrent);
        }
        if (numberCommand == 5)
        {
            Speaker_search(quoteCurrent);
        }
        if (numberCommand == 6)
        {
            quit(quoteCurrent);
        }
    }
    return 0;
}

运行结果及报错内容

D:\C\语录本\语录本.c In function 'quit':
23 3 D:\C\语录本\语录本.c [Warning] implicit declaration of function 'free' [-Wimplicit-function-declaration]
23 3 D:\C\语录本\语录本.c [Warning] incompatible implicit declaration of built-in function 'free'
D:\C\语录本\语录本.c In function 'Quote_add':
30 9 D:\C\语录本\语录本.c [Warning] type defaults to 'int' in declaration of 'numberCommand'
31 2 D:\C\语录本\语录本.c [Warning] implicit declaration of function 'malloc' [-Wimplicit-function-declaration]
31 24 D:\C\语录本\语录本.c [Warning] incompatible implicit declaration of built-in function 'malloc'
D:\C\语录本\语录本.c In function 'Quote_all_show':
54 9 D:\C\语录本\语录本.c [Warning] type defaults to 'int' in declaration of 'numberCommand'
D:\C\语录本\语录本.c In function 'Quote_delete':
67 9 D:\C\语录本\语录本.c [Warning] type defaults to 'int' in declaration of 'numberCommand'
78 2 D:\C\语录本\语录本.c [Warning] incompatible implicit declaration of built-in function 'free'
D:\C\语录本\语录本.c In function 'Quote_modify':
85 9 D:\C\语录本\语录本.c [Warning] type defaults to 'int' in declaration of 'numberCommand'
D:\C\语录本\语录本.c In function 'Speaker_search':
113 9 D:\C\语录本\语录本.c [Warning] type defaults to 'int' in declaration of 'numberCommand'
C:\Users\L25773~1\AppData\Local\Temp\ccrx3Hcg.o 语录本.c:(.rdata$.refptr.numberCommand[.refptr.numberCommand]+0x0): undefined reference to `numberCommand'
D:\C\语录本\collect2.exe [Error] ld returned 1 exit status

我的解答思路和尝试过的方法

看了https://blog.csdn.net/e_t_e_r_n_i_t_y/article/details/109982282

我想要达到的结果

编译通过

  • 写回答

2条回答 默认 最新

  • 慕雪华年 C/C++领域新星创作者 2022-02-03 15:41
    关注

    如果你把函数的实现写在了main函数之前,这一段函数声明是没有必要的

    img


    你在函数里用了free,可是你的这个指针本身就是空的,你没有对它开辟内存,free也是无效的
    自然所有对这个指针的引用都是无效的了!

    img


    提示也提到了,这个指针是空指针,没有进行初始化

    img


    根据你需求初始化一下这个指针指向的结构体就行了

    可以参考一下这个通讯录的写法👉https://blog.csdn.net/qq_61434711/article/details/122720447?utm_source=app&app_version=5.0.1&code=app_1562916241&uLinkId=usr1mkqgl919blen

    如果对你有帮助,还请点个采纳,万分感谢!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 2月3日
  • 已采纳回答 2月3日
  • 创建了问题 2月3日

悬赏问题

  • ¥15 请问为什么我配置IPsec后PC1 ping不通 PC2,抓包出来数据包也并没有被加密
  • ¥200 求博主教我搞定neo4j简易问答系统,有偿
  • ¥15 nginx的使用与作用
  • ¥100 关于#VijeoCitect#的问题,如何解决?(标签-ar|关键词-数据类型)
  • ¥15 一个矿井排水监控系统的plc梯形图,求各程序段都是什么意思
  • ¥50 安卓10如何在没有root权限的情况下设置开机自动启动指定app?
  • ¥15 ats2837 spi2从机的代码
  • ¥200 wsl2 vllm qwen1.5部署问题
  • ¥100 有偿求数字经济对经贸的影响机制的一个数学模型,弄不出来已经快要碎掉了
  • ¥15 数学建模数学建模需要