Binary Tree Help

I am writing program to insert elements of list to Binary Tree. These are the code from three files of project. I am using IDE code::blocks.

TreeNode.h File
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
#ifndef TREENODE_H
#define TREENODE_H

#include<iostream>

template <class Object>
class TreeNode
{
public:
    TreeNode();
    TreeNode(Object*);
    Object* getInfo();
    void setInfo(Object*);
    TreeNode* getLeft();
    void setLeft(TreeNode*);
    TreeNode* getRight();
    void setRight(TreeNode*);
    int isLeaf();

private:
    Object* object;
    TreeNode* left;
    TreeNode* right;
};
#endif // TREENODE_H

TreeNode.cpp File
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
#include "TreeNode.h"

template <class Object>
TreeNode<Object>::TreeNode()
{
    this->object = NULL;
    this->left = this->right = NULL;
}
template <class Object>
TreeNode<Object>::TreeNode(Object* object)
{
    this->object = object;
    this->left = this->right = NULL;
}
template <class Object>
Object* TreeNode<Object>::getInfo()
{
    return this->object;
}
template <class Object>
void TreeNode<Object>::setInfo(Object* object)
{
    this->object = object;
}
template <class Object>
TreeNode<Object>* TreeNode<Object>::getLeft()
{
    return left;
}
template <class Object>
void TreeNode<Object>::setLeft(TreeNode* left)
{
    this->left = left;
}
template <class Object>
TreeNode<Object>* TreeNode<Object>::getRight()
{
    return right;
}
template <class Object>
void TreeNode<Object>::setRight(TreeNode* right)
{
    this->right = right;
}
template <class Object>
int TreeNode<Object>::isLeaf()
{
    if(this->left == NULL && this->right == NULL)
        return 1;
    return 0;
}

main.cpp File
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
#include<iostream>
using std::cout;
using std::endl;

#include "TreeNode.h"

void insert(TreeNode<int>* root, int* info)
{
    TreeNode<int>* node = new TreeNode<int>(info);
    TreeNode<int> *p, *q;
    p = q = root;
    while(*info !=  *(p->getInfo()) && q != NULL)
          {
              p = q;
              if(*info < *(p->getInfo()))
                q = p->getLeft();
              else
                q = p->getRight();
          }
    if(*info == *(p->getInfo()))
    {
        cout<<"Attempt to insert duplicate: "<<*info<<endl;
        delete node;
    }
    else if(*info < *(p->getInfo()))
        p->setLeft(node);
    else
        p->setRight(node);
}

int main()
{
    int x[] = {14, 15, 4, 9, 7, 18, 3, 5, 16, 4, 20, 17, 9, 14, 5, -1};
    TreeNode<int>* root = new TreeNode<int>();
    root->setInfo(&x[0]);
    for(int i = 1; i > x[i]; i++)
    {
        insert(root, &x[i]);
    }

    return 0;
}


But on compiling project the compiler gives following error.
1
2
3
4
5
6
7
8
9
10
11
12
13
||=== TreeNode, Release ===|
obj\Release\main.o:main.cpp|| undefined reference to `TreeNode<int>::TreeNode(int*)'|
obj\Release\main.o:main.cpp|| undefined reference to `TreeNode<int>::getLeft()'|
obj\Release\main.o:main.cpp|| undefined reference to `TreeNode<int>::getInfo()'|
obj\Release\main.o:main.cpp|| undefined reference to `TreeNode<int>::getInfo()'|
obj\Release\main.o:main.cpp|| undefined reference to `TreeNode<int>::getRight()'|
obj\Release\main.o:main.cpp|| undefined reference to `TreeNode<int>::getInfo()'|
obj\Release\main.o:main.cpp|| undefined reference to `TreeNode<int>::getInfo()'|
obj\Release\main.o:main.cpp|| undefined reference to `TreeNode<int>::setRight(TreeNode<int>*)'|
obj\Release\main.o:main.cpp|| undefined reference to `TreeNode<int>::setLeft(TreeNode<int>*)'|
obj\Release\main.o:main.cpp:(.text.startup+0x87)||undefined reference to `TreeNode<int>::TreeNode()'|
obj\Release\main.o:main.cpp:(.text.startup+0xa2)||undefined reference to `TreeNode<int>::setInfo(int*)'|
||=== Build finished: 11 errors, 0 warnings (0 minutes, 2 seconds) ===| 

What is wrong in this code?
you cannot put template definitions into 'TreeNode.cpp' since it must be included before instantiated.
Can you please give me complete code after correction...
Hello.. Anybody.
Help....
Just move everything in the .cpp into the header file. Like coder777 said, you need to actually define the template functions.
Thanks
Topic archived. No new replies allowed.