staticint lookUp(BSTNode<C>* nodePtr, <C> chkVal)
{
if (nodePtr == NULL)
{
//this means the whole tree is empty.
return 0;
}
else
{
if (nodePtr.val == chkVal)
returntrue;
else
{
if (nodePtr.val < chkVal)
lookUp(nodePtr.right, chkVal);
elseif (nodePtr.val > chkVal)
lookUp(nodePtr.left, chkVal);
}
}
}
Can someone direct my in right direction to use those templates used for classes in the friend function. the places i have highlighted are particularly confusing.
Why don't you make BSTNode a full-fledged class with private data and public left() and right() member functions? That way lookup() doesn't have to be anyone's friend.
what exactly is the difference between writing "typename" and "class" . I see you have used typename for friend function and class for the templates in class definition /
@PanGalactic:
ya i could do that... but this way seems a better learning curve exploitation ;) .
#ifndef BST_H
#define BST_H
#include <iostream>
usingnamespace std;
template<class C> struct BSTNode
{
C val;
BSTNode<C>* left;
BSTNode<C>* right;
};
template<class C> class BSTree
{
private:
//BSTNode<C> node;
BSTNode<C>* rootPtr;
protected:
//friend function declaration
template<class T> friendint lookUp(BSTNode<T>*, T); // the function is defined in the class. hence no extra variable is required
public:
//constructors
BSTree<C>();
//desctructors
virtual ~BSTree<C>();
//other functions
int lookMeUpWith(C);
};
//all friend functions to the class BSTree
template<class T> int lookUp(BSTNode<T>*, T);
#endif // BST_H
#include "BST.h"
template<class C> BSTree<C>::BSTree()
{
//ctor
}
template<class C> BSTree<C>::~BSTree()
{
//dtor
}
// other functions
template<class C> int BSTree<C>::lookMeUpWith(C chkVal)
{
// i had to call it this way as i don't want the main.cpp to deal with the struct and node pointers
return (lookUp((rootPtr), chkVal));
}
//all friend function definitions to the class BSTree
template<class T> int lookUp(BSTNode<T>* nodePtr, T chkVal)
{
if (nodePtr == NULL)
{
//this means the whole tree is empty.
return 0;
}
else
{
if (nodePtr.val == chkVal)
returntrue;
else
{
if (nodePtr.val < chkVal)
lookUp(nodePtr.right, chkVal);
elseif (nodePtr.val > chkVal)
lookUp(nodePtr.left, chkVal);
}
}
}
when i try to run this code the errors given is :
In main.cpp : Line 8 : undefined reference to 'BSTree<int>::BSTree()
In main.cpp : Line 10 : undefined reference to 'BSTree<int>::lookMeUpWith(int)
In main.cpp : Line 16 : undefined reference to 'BSTree<int>::~BSTree()
In main.cpp : Line 16 : undefined reference to 'BSTree<int>::~BSTree()
Why is it not being able to find these functions when they already exist.
Is defining a new object of the class the way i have done in main.cpp wrong ?
or is there some other problem ?
the class has a private variable root. Hence if i want to call the function Lookup , i cannot do that since it cannot point to any other nodes in the tree. hence i need another fn which is not a member of class and which can take in a parameter as the pointer to the nodes so that i can call it recursively.