【问题标题】:Error in using Linked List in C program [closed]在 C 程序中使用链表时出错 [关闭]
【发布时间】:2013-04-01 20:28:00
【问题描述】:

我正在为课程做一个额外的学分项目,以下是规格:

  • 你将编写一个 c 程序(你应该使用设计工具,但我不想看到它)
  • 程序将使用动态内存来创建一个链表(不允许使用数组)
  • 该程序将存储无限数量的学生记录(仅受 RAM 限制)。
  • 学生记录将包含学生姓名和年龄……您可能需要添加两个额外的字段才能完成这项工作。
  • 该程序将为用户提供一种添加记录的方法。
  • 该程序将为用户提供一种显示所有记录的方法(仅显示在屏幕上,无需排序)。
  • 程序需要一种退出方式。

我已经完成了所有代码,但我收到了这个讨厌的错误。这正是我在电脑上看到的:

1>linkedProject.obj : error LNK2019: unresolved external symbol _add referenced in    function _main
1>E:\Spring 2013\C Programing Class\linkedProject\Debug\linkedProject.exe : fatal error LNK1120: 1 unresolved externals

这是我的代码:

#include<stdlib.h>
#include<stdio.h>
#include<malloc.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
#define pause system ("pause")

// prototype variables
struct node * initnode(char*, int);
void printnode(struct node*);
void printflist(struct node*);
void add(struct node*);
struct node* searchname(struct node*, char*);

struct node{
    char name[20];
    int age;
    struct node *next;
};

struct node *head = (struct node*) NULL;
struct node *end = (struct node*) NULL;

struct node* initnode(char *name, int age){
    struct node *ptr;
    ptr = (struct node*) calloc(1, sizeof(struct node));
    if(ptr == NULL) 
        return (struct node*) NULL;
    else {
        strcpy(ptr->name, name);
        ptr->age = age;
        return ptr;
     }
}

void printnode(struct node *ptr) {
    printf("Name -> %s\n", ptr->name);
    printf("Age -> %d\n", ptr->age);
}

void printlist (struct node *ptr) {
    while (ptr != NULL) {
        printnode(ptr);
        ptr = ptr->next;
    }
}



main() {
    char name[20];
    int age, choice = 1;
    struct node *ptr;
    while(choice != 3){
        system("cls");
        printf("1. Add a name\n");
        printf("2. List all names\n");
        printf("3. Exit");
        printf("\nEnter Menu Selection: ");
        scanf("%d", &choice);
        switch(choice) {
        case 1: printf("\nEnter a name: ");
            scanf("%s", &name);
            printf("Enter age: ");
            scanf("%d", &age);
            ptr = initnode(name, age);
            add (ptr);
            break;
        case 2: printlist(head);
            break;
        case 3: exit(3);
        default: printf("Invalid Entry");
        }// end of switch


    }// end of while

}// end of main

非常感谢所有帮助!

【问题讨论】:

  • 答案是add (ptr);是什么?

标签: c error-handling linked-list dynamic-memory-allocation singly-linked-list


【解决方案1】:

链接器告诉您它找不到add 函数。

您已声明:

void add(结构节点*)

在您的原型中,但您尚未在任何地方定义它。

【讨论】:

    【解决方案2】:

    您在prototype 中声明add 方法。和 add(ptr) call 在 main 方法中。但我看不到add method definiton。这就是为什么,编译器会出现链接器错误。

    【讨论】:

      猜你喜欢
      • 2016-12-20
      • 2016-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多