identifier not found problem
Apr 11, 2012 at 4:02pm UTC
I'm not sure what i'm doing wrong, any help would be great.
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
//treenode.h
#ifndef TREENODE_H
#define TREENODE_H
template <typename T>class tree;
template <typename T>
class treeNode
{
friend class tree<T>;
public :
//constructor
treeNode(const T &d) : left(0), right(0), data(d) {}
T getData() const
{
return data;
}
private :
treeNode<T> *left;
T data;
treeNode<T> *right;
};
#endif
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
//tree.h
#ifndef TREE_H
#define TREE_H
#include "treenode.h"
template <typename T>
class tree
{
public :
tree();
void insertNode(const T &);
void preOrderTraversal() const ;
void inOrderTraversal() const ;
void postOrderTraversal() const ;
private :
treeNode<T> *root;
//utility functions
void insertNodeHelper(treeNode<T> **, const T &);
void preOrderHelper(treeNode<T> *) const ;
void inOrderHelper(treeNode<T> *) const ;
void postOrderHelper(treeNode<T> *) const ;
};
#endif
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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
//main.cpp
#include <iostream>
#include <conio.h>
#include "tree.h"
using namespace std;
//constructor
template <typename T>
tree<T>::tree()
{
root = 0;
}
//insert node in tree
template <typename T>
void tree<T>::insertNode(const T &value)
{
insertNodeHelper(&root, value);
}
//utility function called by insertNode; receives a pointer
//to a pointer so that the function can modify pointer's value
template <typename T>
void tree<T>::insertNodeHelper(treeNode<T> **ptr, const T &value)
{
//if subtree is empty, create a new treeNode containing value
if (*ptr == 0)
*ptr = new treeNode<T>(value);
else
{
//subtree is not empty
if (value < (*ptr)->data)
insertNodeHelper(&((*ptr)->left), value);
else
{
if (value > (*ptr)->data)
insertNodeHelper(&((*ptr)->right), value);
else
cout << value << " dup" << endl;
}
}
}
//begin preorder traversal of tree
template <typename T>
void tree<T>::preOrderTraversal() const
{
preOrderHelper(root);
}
//utility function to perform preorder traversal of tree
template <typename T>
void tree<T>::preOrderHelper(treeNode<T> *ptr) const
{
if (ptr != 0)
{
cout << ptr->data << ' ' ;
preOrderHelper(ptr->left);
preOrederHelper(ptr->right);
}
}
//begin inordertraversal of tree
template <typename T>
void tree<T>::inOrderTraversal() const
{
inOrderHelper(root);
}
//utility function to perform inorder traversal of tree
template <typename T>
void tree<T>::inOrderHelper(treeNode<T> *ptr) const
{
if (ptr != 0)
{
inOrderHelper(ptr->left);
cout << ptr->data << ' ' ;
inOrderHellper(ptr->right);
}
}
//begin post order traversal of tree
template <typename T>
void tree<T>::postOrderTraversal() const
{
postOrderHelper(root);
}
//utility function to perform postorder traversal of tree
template <typename T>
void tree<T>::postOrderHelper(treeNode<T> *ptr) const
{
if (ptr != 0)
{
postOrderHelper(ptr->left);
postOrderHelper(ptr->right);
cout << ptr->data << ' ' ;
}
}
int main()
{
tree<int > intTree;
int intValue;
cout << "Enter 10 integer values: " << endl;
for (int i = 0; i < 10; i++)
{
cin >> intValue;
intTree.insertNode(intValue);
}
cout << endl << endl;
cout << "Preorder traversal\n" ;
intTree.preOrderTraversal();
cout << "\nInOrder traversal\n" ;
intTree.inOrderTraversal();
cout << "\nPostOrder traversal\n" ;
intTree.postOrderTraversal();
_getch();
return 0;
}
Apr 11, 2012 at 4:38pm UTC
If you would like help, please copy and paste the errors you are getting.
Apr 11, 2012 at 6:38pm UTC
These are the errors that I got when I tried to compile this (next time, please do this yourself, okay?)
main.cpp: In member function 'void tree<T>::preOrderHelper(treeNode<T>*) const [
with T = int]':
main.cpp:48:2: instantiated from 'void tree<T>::preOrderTraversal() const [wit
h T = int]'
main.cpp:117:28: instantiated from here
main.cpp:59:3: error: 'preOrederHelper' was not declared in this scope
main.cpp: In member function 'void tree<T>::inOrderHelper(treeNode<T>*) const [w
ith T = int]':
main.cpp:67:2: instantiated from 'void tree<T>::inOrderTraversal() const [with
T = int]'
main.cpp:120:27: instantiated from here
main.cpp:78:3: error: 'inOrderHellper' was not declared in this scope
Basically, looks like you had a typo here:
52 53 54 55 56 57 58 59 60 61
template <typename T>
void tree<T>::preOrderHelper(treeNode<T> *ptr) const
{
if (ptr != 0)
{
cout << ptr->data << ' ' ;
preOrderHelper(ptr->left);
preOrederHelper(ptr->right); // Here
}
}
and here:
71 72 73 74 75 76 77 78 79 80
template <typename T>
void tree<T>::inOrderHelper(treeNode<T> *ptr) const
{
if (ptr != 0)
{
inOrderHelper(ptr->left);
cout << ptr->data << ' ' ;
inOrderHellper(ptr->right); // Here
}
}
Topic archived. No new replies allowed.