Undefined reference error

Hello. I am working on an assignment in which a program takes in a passage through standard input, stores each word in a binary tree and counts the words frequency within the passage, and then prints the words and their frequency out in alphabetical order.

I am supposed to use multiple files and I am supposed to create the tree functions and put them in a .h file. When I compile my program with the functions and their prototypes in one .cpp file it works fine, but when I separate them into multiple files I get an undefined reference error.

Other information: I am using the IDE code blocks with the MinGW compiler.
Here is my code:

trees.h
1
2
3
4
5
6
7
8
9
10
#include <string>
struct tNode {
    std::string data;
    int count;
    tNode *left, *right;
};

void insert(std::string data, tNode *&root);
void inOrder(tNode *root);
std::string fixString(std::string data);


trees.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <string>
#include "trees.h"
using namespace std;

void insert(string data, tNode *&root)
{
    if(root==NULL)
    {
        tNode *t = new(tNode);
        t->data = data;
        t->left = t->right = NULL;
        t->count = 0;
        root = t;
    }
    if(data==root->data)
    {
        root->count++;
        return;
    }
    else if(data<root->data)
    {
        insert(data, root->left);
    }
    else
    {
        insert(data, root->right);
    }
}
void inOrder(tNode *root)
{
    if(root==NULL)
    {
        return;
    }
    inOrder(root->left);
    cout<<root->data<<" "<<root->count<<endl;
    inOrder(root->right);
}

string fixString(string data)
{
    int length;
    length=data.length();
    if(ispunct(data[length-1]))
    {
        data.erase(length-1);
    }
    for(int i=0;i<length;i++)
    {
        data[i]=tolower(data[i]);
    }
    return data;
}


proj3.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <stdio.h>
#include <ctype.h>
#include <string>
#include "trees.h"

using namespace std;

int main()
{
    string data;
    int length;
    tNode *root = NULL;

    cout<<"Enter passage to be analyzed. Enter QUIT when finished:"<<endl;
    while(cin>>data, data!="QUIT")
    {
        data = fixString(data);
        insert(data, *&root);
    }
    inOrder(root);
    return(0);
}


If anyone can figure this out I will be very grateful.
Last edited on
Please post the entire error message.
If I had to guess, you have not told your IDE that all the separate cpp files are part of the same "project" or whatever your IDE calls it.
Sorry, these are the errors I get:

undefined reference to 'fixString(std::string)'
undefined reference to 'insert(std::string, tNode*&)'
undefined reference to 'inOrder(tNode*)'
It looks like module trees.cpp was not included in the project.
Hmm...You're right. Can't believe I overlooked that. Works now, thank you so much.
Topic archived. No new replies allowed.