0

I am having an issue with my program that cross-references a file and displays certain information in an alphabetized, numbered list. I keep getting 2 errors stating "undefined reference" for newMyTree as well as findOrInsert. I really don't know what I'm doing wrong and it is very frustrating. Can someone please help? These are the errors:

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)) ===|

The code:

#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
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Tanya
  • 3
  • 2
  • 1
    Have you written the code for the functions `newMyTree()` and `findOrInsert()`? Can the compiler (or the linker if they are previously compiled) find that code? – pmg Mar 16 '14 at 22:57
  • @500-InternalServerError I tried that but it didn't work. Thanks for the suggestion though. – Tanya Mar 16 '14 at 22:57
  • @pmg I am unsure. It may be that I haven't previously defined them but I am unsure how. – Tanya Mar 16 '14 at 22:58
  • possible duplicate of [Beginner receiving an errors stating "undefined reference" in my C code](http://stackoverflow.com/questions/22443382/beginner-receiving-an-errors-stating-undefined-reference-in-my-c-code) – jpw Mar 16 '14 at 22:58
  • @jpw That was my question. I just reworded it and added the errors and posted it again. – Tanya Mar 16 '14 at 22:59
  • @Tanya Yes, I realize that. It would have been better to edit the original question instead of posting a new almost identical question. – jpw Mar 16 '14 at 23:01
  • @jpw This is my first time using stackoverflow and I didn't know how to edit the question so I re-posted it. I apologize for any confusion it may have caused. – Tanya Mar 16 '14 at 23:02
  • If you have the code for those functions written in a different file, you may need to specify 2 file names for the compiler to fit everything together – pmg Mar 16 '14 at 23:04
  • @pmg I don't have code for those functions in a different file. Everything is in one file. – Tanya Mar 16 '14 at 23:15
  • Then, as diagnosed in the answer, your problem is that 'Everything' is missing two functions — neither `newMyTree()` nor `findOrInsert()` is defined in the code you show, and the compiler is telling you that they are missing. – Jonathan Leffler Mar 16 '14 at 23:27
  • Since this question has a reasonable answer at least pointing in the right direction and the other question does not, I nominate the other question as a duplicate of this rather than vice versa, even though this was asked later. – Jonathan Leffler Mar 16 '14 at 23:45
  • So you're just posting a slew of code you didn't write, the code is incomplete, and you have no idea how to write the missing code. Good luck with that. – Jim Balter Mar 17 '14 at 00:17
  • @500-InternalServerError "Missing space after new in newMyTree?" -- Of course not; that would make absolutely no sense even if this were tagged C++ ... which it isn't. – Jim Balter Mar 17 '14 at 00:22
  • @JimBalter I did write this code and I clearly stated before that I am a beginner to C. I am aware the code is incomplete, I did not post all of it. Please refrain from any future comments on my question as your responses are unhelpful and antagonistic. – Tanya Mar 17 '14 at 00:35
  • First, why didn't you post all of it? Second, that's irrelevant -- the point is that the functions that are undefined are missing from your full source -- that's why you got undefined references. If you wrote it, how could you possibly not know that these functions that you refer to remain unwritten? You asked Jonathan Leffler how you would go about writing the missing functions ... that strongly suggests that you didn't write the rest. As for the rest, go stuff it. Do you really think you, who just showed up here, can tell experienced contributors where they can comment?? – Jim Balter Mar 17 '14 at 01:08
  • possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – Luchian Grigore Apr 09 '14 at 11:05

1 Answers1

1

First, in your main function you have a bunch of function prototypes:

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);

They should go before the main block, but after the various structs you define.

The errors you are getting are is because the compiler can't find the functions findOrInsert and newMyTree anywhere. If you have written them in another file (a header for example) you need to include that file in this with a #include "your_header.h" at the top. Either that or provide an implementation for them in your .c file like you have done with getWord and inOrder for instance.

jpw
  • 44,361
  • 6
  • 66
  • 86
  • can you give me an example? @jpw – Tanya Mar 16 '14 at 23:26
  • @Tanya: what do you need an example of? The function declarations inside `main()` should be outside `main()`, after the sequence of `typedef` statements. The missing functions simply need to be written, the same as the functions that are already present. – Jonathan Leffler Mar 16 '14 at 23:30
  • @Tanya Look at what you've already done with the `getWord` function. You need to do the same thing with the two missing functions, but oviously with different functionality. – jpw Mar 16 '14 at 23:33
  • @JonathanLeffler Im enquiring as to how I would write the missing functions. I have literally no prior background in C so a lot of this is very confusing for me. – Tanya Mar 16 '14 at 23:33
  • 1
    @Tanya: Well, one starts as `NodeData newNodeData(char str[]) { … }` and the other starts as `MyTreePtr findOrInsert(BinaryTree bt, NodeData data) { … }`, just based on the declarations. What replaces the `…` is not something we can answer; we don't know what you think they ought to do. You've not given a specification for what the functions are intended to do, so we can't write them. We shouldn't write the code even if you _do_ supply the specification; you won't learn if we do it for you. We'll help you if you run into problems with your implementation, but we don't write code de novo. – Jonathan Leffler Mar 16 '14 at 23:40