c - 在我的 C 代码中收到 2 个错误,指出 “undefined reference”

标签 c compiler-errors syntax-error codeblocks undefined-reference

我的程序存在问题,该程序交叉引用文件并在按字母顺序排列的编号列表中显示某些信息。我不断收到 2 个错误,指出 newMyTree 以及 findOrInsert“ undefined reference ”。我真的不知道我做错了什么,这非常令人沮丧。有人可以帮忙吗?这些是错误:

C:\**\**\**\**\crossref.o:crossref.c|| undefined reference to newMyTree|
C:\**\**\**\**\crossref.o:crossref.c|| undefined reference to findOrInsert| 

||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 3 second(s)) ===|

代码:

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#define MaxWordSize 20
#define MaxLine 101    

typedef struct listLinked{
    int numLine;
    struct listLinked*next;
    }ListLinked,*ListLinkedPtr;

typedef struct{
    char word[MaxWordSize+1];
    ListLinkedPtr firstLine;
    }NodeData;

typedef struct MyTree{
    NodeData data;
    struct MyTree*left,*right;
    }MyTree,*MyTreePtr;    

typedef struct{
    MyTreePtr root;
    }BinaryTree;

main(){
    int getWord(char[], char[]);
    MyTreePtr newMyTree(NodeData);
    NodeData newNodeData(char[]);
    MyTreePtr findOrInsert(BinaryTree, NodeData), node;
    ListLinkedPtr newListLinked(int);
    void inOrder(FILE*, MyTreePtr);

    char word[MaxWordSize+1];
    char line[MaxLine];
    int currentLine = 0;

    FILE*in = fopen("passage.in","r");
    FILE*out = fopen("passage.out","w");
    BinaryTree bst;
    bst.root = NULL;

    while (fgets(line, MaxLine, in)!= NULL){
        fprintf(out,"%3d. %s\n",++currentLine, line);
        //extract words from current line
        while (getWord(line, word)!= 0){
            if (bst.root == NULL)
                bst.root = node = newMyTree(newNodeData(word));
            else
                node = findOrInsert(bst,newNodeData(word));
            ListLinkedPtr ptr = newListLinked(currentLine);
            ptr -> next = node -> data.firstLine;
            node -> data.firstLine = ptr;
            }
        }
        fprintf(out, "\nWords               Line numbers\n\n");
        inOrder(out, bst.root);
        fclose(in); fclose(out);
}//close main

int getWord(char line[], char str[]){
//finds the next word in line and stores it in str
//returns 1 if a word is found; 0 otherwise
    static int p = 0; //p retains its value between calls to getWord
    char ch;
    int n = 0;
    //skips over non-letters
    while (line[p]!= '\0' &&!isalpha(line[p]))p++;
    if (line[p]!='\0')return p = 0; //reset p for the next line
    str[n++] = tolower(line[p++]);
    while (isalpha(line[p])){
        if(n < MaxWordSize)str[n++]= tolower(line[p]);
        p++;
    }
    str[n]= '\0';
    return 1;
}//end getWord

void inOrder(FILE*out, MyTreePtr node){
    void printAWord(FILE*, MyTreePtr);
    if (node!= NULL){
        inOrder(out, node -> left);
        printAWord(out, node);
        inOrder(out, node -> right);
    }
}//end inOrder

void printAWord(FILE* out, MyTreePtr pt){
    void printLineNumbers(FILE*,ListLinkedPtr);
    fprintf(out,"%-20s", pt -> data.firstLine -> next); //print all except first
    fprintf(out, "%3d\n", pt -> data.firstLine -> numLine);//print first
}//end printAWord

void printLineNumbers(FILE* out, ListLinkedPtr top){
//line numbers are in reverse order; print list reversed
    if (top != NULL){
        printLineNumbers(out, top -> next);
        fprintf(out, "%3d,", top -> numLine);
    }
}//end printLineNumbers

NodeData newNodeData(char str[]){
    NodeData temp;
    strcpy(temp.word, str);
    temp.firstLine = NULL;
    return temp;
}//end newNodeData

ListLinkedPtr newListLinked(int lineNo){
    ListLinkedPtr p = (ListLinkedPtr) malloc(sizeof(ListLinked));
    p -> numLine = lineNo;
    p -> next = NULL;
    return p;
}//end of newListLinked

最佳答案

首先,在你的主函数中你有一堆函数原型(prototype):

int getWord(char[], char[]);
MyTreePtr newMyTree(NodeData);
NodeData newNodeData(char[]);
MyTreePtr findOrInsert(BinaryTree, NodeData); // this line had some typos...
ListLinkedPtr newListLinked(int);
void inOrder(FILE*, MyTreePtr);

它们应该位于 main block 之前,但在您定义的各种结构之后。

您收到的错误是因为编译器无法在任何地方找到函数 findOrInsertnewMyTree 。如果您已将它们写入另一个文件(例如 header )中,则需要将该文件包含在其中,并在顶部添加 #include "your_header.h"。或者在您的 .c 文件中为它们提供一个实现,就像您对 getWordinOrder 所做的那样。

关于c - 在我的 C 代码中收到 2 个错误,指出 “undefined reference”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22444171/

相关文章:

c++ - 使用 db2 和 c++ 在数据库中存储十进制浮点值 (DECFLOAT)

c - printf 函数在输入之前不起作用

php - MySQL查询无法运行

c++ - 结构中的字符

c - 没有 malloc 时发生内存泄漏

c++ - 错误 C2100 - 非法间接

delphi - 在传递给另一个函数的匿名方法中使用开放数组参数时出现“无法捕获符号”错误

c++ - 对C++中函数的 undefined reference

php - 语法错误,意外的T_STRING,期望 ']'

Flutter "argument type not assignable"两种相同类型的错误