After declaring the class instance "node" as I did above, I now am getting new errors:
myavltreeTest.cpp: In function 'int main()':
myavltreeTest.cpp:9:17: error: no matching function for call to 'Avltree<int>::Avltree()'
myavltreeTest.cpp:9:17: note: candidates are:
myavltree.h:12:7: note: Avltree<T>::Avltree(T) [with T = int]
myavltree.h:12:7: note: candidate expects 1 argument, 0 provided
myavltree.h:6:7: note: Avltree<int>::Avltree(const Avltree<int>&)
myavltree.h:6:7: note: candidate expects 1 argument, 0 provided
The code after fixing the typos is as follows:
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
|
//Header file:
template <typename T>
class Avltree
{
public:
//Creates a new avl tree main root and stores the data at the main root.
//Also stores the values of NULL into the parent, left child and
//right child.
Avltree<T>(T data)
{
leftChild = rightChild = parent = NULL;
this->data = data;
}
//Returns the address of the parent if there is one, else it returns
//null.
Avltree<T> *getParent()
{
return parent;
}
//Returns the address of the left child if there is one, else it returns
//null.
Avltree<T> *getLeftChild()
{
return leftChild;
}
//Returns the address of the right child if there is one, else it returns
//null.
Avltree<T> *getRightChild()
{
return rightChild;
}
private:
Avltree<T> *parent;
Avltree<T> *leftChild;
Avltree<T> *rightChild;
T data;
};
#endif
//Main file
int main()
{
Avltree<int> *root = NULL;
Avltree<int> node;
int data;
do
{
cout << "Enter Number (-1 to exit): ";
cin >> data;
if(root == NULL)
root = new Avltree<int>(data);
cout << endl;
cout << "root = " << root << endl;
cout << "leftChild = " << node.getLeftChild() << endl;
cout << "rightChild = " << node.getRightChild() << endl;
cout << endl;
|
This is just baffling to me, as well as very confusing. I'm not sure what to look at next, because I also thought too that after declaring the instance, I could use the dot operator to access the class members.