By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,463 Members | 2,954 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,463 IT Pros & Developers. It's quick & easy.

Link Error, Why doesn't my simple binary search tree class work??

P: n/a
I have the following code:

Tree.h defines a simple binary search tree node structure
########## FILE Tree.h ################
#ifndef TREE_H
#define TREE_H
//using namespace std;
template <typename Tclass Tree{
private:
Tree<T*left;
Tree<T*right;
T data;
public:
Tree(const T& item, Tree<T>* lptr, Tree<T>*
rptr):data(item),left(lptr),right(rptr){};
Tree<T>* Left() const;
Tree<T>* Right() const;
};

#endif
#######################################
Tree.cpp implemented Tree<T>* Left() const and Tree<T>* Right() const
###############FILE Tree.cpp ###########
#include "Tree.h"
#include <iostream>
using namespace std;

template <typename TTree<T>* Tree<T>::Left() const{
return left;
}

template <typename TTree<T>* Tree<T>::Right() const{
return right;
}
########################################

Tree_Scan.h defines a "inorder" scan function on the binary search tree
################## FILE Tree_Scan.h#########
#ifndef TREE_SCAN_H
#define TREE_SCAN_H

#include "Tree.h"
//using namespace std;

template <typename Tvoid inorder(Tree<T>*);

#endif
###########################################

Tree_Scan.cpp implemented "inorder" function
#################FILE Tree_Scan.cpp#########
#include "Tree.h"
#include "Tree_Scan.h"
#include <iostream>
using namespace std;

template <typename Tvoid inorder(Tree<T>* tree){
if(tree != NULL){

inorder(tree->left());

cout << (*tree).data << endl;

inorder(tree ->right());
}
}
###########################################

In the main function, first a binary search tree is built and then
perform an "inorder" scan
on it
##################FILE main.cpp#############
#include "Tree.h"
#include "Tree_Scan.h"
#include <string>
using namespace std;

int main(){
Tree<string*g = new Tree<string>("G", NULL, NULL);
Tree<string*h = new Tree<string>("H", NULL, NULL);
Tree<string*i = new Tree<string>("I", NULL, NULL);
Tree<string*f = new Tree<string>("F", NULL, NULL);

Tree<string*d = new Tree<string>("D", NULL, g);
Tree<string*b = new Tree<string>("B", d, NULL);
Tree<string*e = new Tree<string>("E", h, i);
Tree<string*c = new Tree<string>("C", e, f);
Tree<string*a = new Tree<string>("A", b, c);

inorder(a);

return 1;
}
##################################################

When linking a got a link error, complain that there is something wrong
with "inorder(a)", I've
checked my program several times and find no problem, why it fails?
Thanks for helping me!

Jan 1 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
hn********@gmail.com wrote:
[full code -- thank you, by the way -- redacted]
Linker error on templated function.

This is a FAQ.

See http://www.parashift.com/c++-faq-lit...html#faq-35.13
Jan 1 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.