致命错误LNK1120:C中有1个未解析的外部

时间:2012-11-21 08:55:13

标签: c

通常当我收到此错误时,创建一个新项目并复制粘贴代码修复,但现在却没有。

这是输出:

1>------ Build started: Project: myList, Configuration: Debug Win32 ------
1>  List.cpp
1>List.obj : error LNK2019: unresolved external symbol "void __cdecl printStd(struct Student >*)" (?printStd@@YAXPAUStudent@@@Z) referenced in function _main
1>C:\Users\Talmid\Desktop\אמרי\C\myList\Debug\myList.exe : fatal error LNK1120: 1 unresolved >externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

代码:

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

#define SUCCESS 1
#define FAILURE 0

// A definition of some information type:
typedef struct
{
char name[20];
int id;
}Student;

// A definition of a single node
typedef struct node_s
{
Student data;
struct node_s *next;
}Node;

// a definition of "list manager":
typedef struct
{
Node *start;
int num;
}List;

List *createList();
int insertToList(List *ptrList, Student *ptrStu);
int removeFirst(List *ptrList, Student *ptrStu);
Node *getRecord(List *ptrList, int recNum);
void printList(List *ptrList);
void printRec(Node *ptrNode);
void printStd(Student *std);
void freeList(List *ptrList);
void shutDown(List *ptrList);

//Creates a new 'list manager', initializes it and returns its value;
List *createList()
{
    List *ptrNewList = (List *)malloc(sizeof(List));
    if(ptrNewList)
    {
        ptrNewList->start = NULL;
        ptrNewList->num = 0;
    }
    return ptrNewList;
}

// free 'list manager'
void shutDown(List *pList)
{
    if(pList != NULL)
        free(pList);
}

// inserts one record a the head of the list
int insertToList(List *ptrList, Student *ptrStu)
{
    Node *ptrNewNode = (Node *)malloc(sizeof(Node));
    if(ptrNewNode)
    {
        ptrNewNode->data = *ptrStu;
        ptrNewNode->next = ptrList->start;
        ptrList->start = ptrNewNode;
        ptrList->num++;
        return SUCCESS;
    }
    return FAILURE;
}

// remooves the first node from the list
int removeFirst(List *ptrList, Student *ptrStu)
{
    Node *ptrDel;
    if(ptrList->start != NULL)
    {
        ptrDel = ptrList->start;
        ptrList->start = ptrDel->next;
        ptrList->num--;
        *ptrStu = ptrDel->data;
        free(ptrDel);
        return SUCCESS;
    }
    return FAILURE;
}

// gets the data of the recNum node
Node *getRecord(List *ptrList, int recNum)
{
    Node *ptrNode;
    int nodeNum = 1;
    ptrNode = ptrList->start;
    while((nodeNum<recNum) && (ptrNode != NULL))
    {
        ptrNode = ptrNode->next;
        nodeNum++;
    }
    return ptrNode;
}

// prints the entire list
void printList(List *ptrList)
{
    Node *ptrNode;
    int i;
    ptrNode = ptrList->start;

    i = 1;
    while(ptrNode != NULL)
    {
        printf("%2d: ",i);
        printRec(ptrNode);
        ptrNode = ptrNode->next;
        i++;
    }
    printf("\n");
}

// prints a given record
void printRec(Node *ptrNode)
{
    printf("name is %10s, id is %d.\n", ptrNode->data.name, ptrNode->data.id);
}

// prints a given student
void printSt(Student *std)
{
    printf("name is %10s, id is %d.\n", std->name, std->id);
}

//free all nodes (dynamic allocation)
void freeList(List *ptrList)
{
        Node *ptrDel;
    if(ptrList != NULL)
    {
        ptrDel = ptrList->start;
        while(ptrDel)
        {
            ptrList->start = ptrDel->next;
            free(ptrDel);
            ptrDel = ptrList->start;
        }
    }
}

void main()
{
    List *l1 = NULL;
    Node *n1 = NULL;
    Student *s1 = NULL;
    int i, status;
    int wantedIndexElement = 3;
    Student studentsArr[] = {
                                {"Rina",134},
                                {"Tomer",22307},
                                {"Rotem",3732},
                                {"Yosef",773},
                                {"Anat",9998},
                                {"Vered",14555},
                                {"Malkishua",878},
                                {"David",6543},
                                {"Yigal",9870},
                                {"Beni",123}
                            };
    int arrLen = sizeof(studentsArr)/sizeof(Student);
    l1 = createList();
    if(l1 == NULL)
    {
        printf("problem to allocate List (list manager)\n");
        exit(1);
    }
    for(i=0;i<arrLen;i++)
    {
        status = insertToList(l1,&studentsArr[i]);
        if(status == FAILURE)
        {
            printf("Failed to insert to the list!!!\n");
            exit(1);
        }
    }
    printList(l1);
    n1 = getRecord(l1,wantedIndexElement);

    if(n1 != NULL)
        printRec(n1);
    else
        printf("No such element %d \n",wantedIndexElement);



    s1 = (Student *)malloc(sizeof(Student));
    if (removeFirst(l1,s1) == FAILURE)
    {
        printf("Failed to remove from list!!!\n");
            exit(1);
    }

    printf("The   student has been deleted:\n");
    printStd(s1);


    freeList(l1);
    shutDown(l1);
    l1 = NULL;
    putchar('\n');
    getchar();
}

由于它是一个很长的代码,你真的不需要检查它,没有错误..甚至控制台都这么说:D

那么这个错误意味着什么?为什么我会得到它?我该如何解决?它引导我到一个甚至不存在的exe。

谢谢

2 个答案:

答案 0 :(得分:1)

void printStd(Student * std) - &gt;这就是你宣布的内容。

void printSt(Student * std) - &gt;这是您编写的函数名称

“未解析的外部符号”void __cdecl printStd“编译器告诉您它找不到任何已经decalred的名为printStd的函数。

答案 1 :(得分:0)

你有一个名为printStd的函数的原型,但没有正文:

void printStd(Student *std);

printSt函数有一个正文。这应该是printStd吗?