Compilation of header file for a class/

Hi all,

I am having a problem understanding the concept of a header file. I thought that a header file was used for the declaration and possible definition of function prototypes and classes. I know if I make a third file that contains the definitions of member functions for a class, then everything should work. However, I am getting an error that reads:

leftChild was not declared in this scope.
rightChild was not declared in this scope.

Any thoughts or insights as to why it is not recognizing the two functions?
Any and all help is greatly appreciated. Thank you.

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

//The main file to call functions and create the avl tree
#include <iostream>
#include "myavltree.h"

using namespace std;

int main()
{
   Avltree<int> *root = NULL;
   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 = " << leftChild() << endl;
      cout << "rightChild = " << rightChild() << endl;
      cout << endl;


//Header file for the avl class

#ifndef MYAVLTREE_H
#define MYAVLTREE_H
using namespace std;

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
What are lines 23 and 24 supposed to be doing? Your compiler is right, you never made any functions ever called leftChild or rightChild.
Thank you for the reply. After posting, I realized that I had some typos to fix, however when I made the functions finally match, the same errors would occur again. On lines 23 and 24, I am only trying to see what the value is stored in the leftChild and rightChild member variables. And, only because I am having a difficult time understanding pointers.
What is your current code? You need an instance of an object to call a member function, you can just write the name of the member function alone and expect the compiler to magically know which object you're referring to.
My current code is the same as above except that I only added the word "get" in front of the leftChild and rightChild function invocations in main, as well as fixed the typo of the lowercase letters. When you say that I need an instance of an object to call a member function, are you referring to say:

int main()
{
Avltree<int> node; //Creation of instance to call member function

node.getLeftChild();
}

Or am I missing the concept all together?
Yes, on lines 23 and 24 you want to use root.getLeftChild() and root.getRightChild() ;)
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.
The error is because your class has no default consructor.

How is your node variable supposed to be related to your root variable in any way?
Honestly, at this point, I'm not exactly sure. I'm going to make a default constructor and see if it will compile. This is for a data structures courses and the instructor has given us half of the code to use. However, I have a difficult time understanding what his does and more so how it works. So I thought about trying to write my own avl tree, but as you can see, its working out like a fart in a diving helmet :)
Even if it compiles it will not do what you want.
Topic archived. No new replies allowed.