-1

我正在上课的课外项目,在这里使用链表是规格:错误在C程序

  • 你会写C语言程序(你应该使用一个设计工具,但我不希望看到它)
  • 该程序将使用动态内存来创建一个链接列表(NO NORRAYS PERMITTED)
  • 该程序将存储无限数量的学生记录(仅限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 

所有帮助是极大的赞赏!

+0

答案是'add(ptr);'? – 2013-04-10 06:25:36

回答

1

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

您已经声明:

空加(结构节点*)

在你的原型

,但是你有没有在任何地方定义它。

1

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