473,748 Members | 9,933 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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),ri ght(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
1 2950
hn********@gmai l.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
3651
by: pembed2003 | last post by:
Hi, I have a question about how to walk a binary tree. Suppose that I have this binary tree: 8 / \ 5 16 / \ / \ 3 7 9 22 / \ / \ / \
0
4253
by: j | last post by:
Hi, Anyone out there with binary search tree experience. Working on a project due tomorrow and really stuck. We need a function that splits a binary tree into a bigger one and smaller one(for a random binary search tree. We've tried everything but are in the land of pointer hell. If someone could help it would be a huge help. our code follows. We've tried 2 diff methods the split() and splitter() functions #include <iostream> #include...
2
5310
by: dannielum | last post by:
Hi all, I am trying to write a Binary Search Tree that each of its node will have 3 node pointers: left, right and parent. I need a parent pointer for some the purpose of my project. Without the pointer to the parent node, the program will be inefficient and slow. It works fine at first. However, when I started to build the remove function, it destroys the tree when I delete a node. I already changed the parent pointer whenever I delete a...
15
5094
by: Foodbank | last post by:
Hi all, I'm trying to do a binary search and collect some stats from a text file in order to compare the processing times of this program (binary searching) versus an old program using linked lists. I'm totally new to binary searches by the way. Can anyone help me with the commented sections below? Much of the code such as functions and printfs has already been completed. Any help is greatly appreciated. Thanks,
10
9781
by: free2cric | last post by:
Hi, I have a single link list which is sorted. structure of which is like typedef struct mylist { int num; struct mylist *next;
1
2510
by: mathon | last post by:
hi, now i facing a problem which i do not know how to solve it...:( My binary search tree structures stores a double number in every node, whereby a higher number is appended as right child and a less or equal number is appended as a left child. Now i want to write a function which deletes the node with the highest number in the tree. I started the function as follows:
4
2171
by: BenCoo | last post by:
Hello, In a Binary Search Tree I get the error : Object must be of type String if I run the form only with the "Dim bstLidnummer As New BinarySearchTree" it works fine. Thanks for any help on this, Benny
5
5273
gekko3558
by: gekko3558 | last post by:
I am writing a simple binary search tree (nodes are int nodes) with a BSTNode class and a BST class. I have followed the instructions from my C++ book, and now I am trying to get a remove method working. But before I get to the remove, I need to get my find method working. Basically, I am trying to get a "find" method working that will search for a giving int value, and return the node with that value. I have designed my current find with the...
11
1191
by: Defected | last post by:
Hi, How i can create a Binary Search Tree with a class ? thanks
0
8991
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8831
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9548
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9374
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8244
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6796
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4607
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2787
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2215
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.