Question about inheritance.

If I have a class lets call it BT. And class BST inherits BT. And then class AVL inherits class BST. Can AVL use the functionality of BST and BT? And if it can how would I go about doing that.

The errors look something like this.

test_avl.cpp:(.text._ZN3BSTIiED2Ev[BST<int>::~BST()]+0xd): undefined reference to `BT<int>::~BT()'
I am not calling the destructor but it is called automatically. But anytime I try to call a function in BST or BT it gives me an undefined reference to...

I think that it is just syntax that I don't know, I don't think there is anything wrong with the code other than that.


Last edited on
It sounds like you have defined a destructor in the class but not actually given it a definition.
Well here is the 3 classes.

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
template<typename generic>
class BT
{
 public:
  BT();
  BT(const BT& c);
  ~BT();
  BT& operator=(const BT& rhs);
  void clear();
  bool empty() const;
  unsigned int size() const;
  typedef BTPreorderIterator<generic> PreOrder;
  typedef BTInorderIterator<generic> InOrder;
  typedef BTPostorderIterator<generic> PostOrder;
  PreOrder pre_begin() const;
  PreOrder pre_end() const;
  InOrder in_begin() const;
  InOrder in_end() const;
  PostOrder post_begin() const;
  PostOrder post_end() const;
  
 protected:
  BTN<generic>* m_root;
  unsigned int m_size;
};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
template<typename generic>
class BST : public BT<generic>
{
 public:
  void insert(generic x);      
  void remove(generic x);
  typedef BTPreorderIterator<generic> PreOrder;
  typedef BTInorderIterator<generic> InOrder;
  typedef BTPostorderIterator<generic> PostOrder;
  PreOrder pre_search(generic x);
  InOrder in_search(generic x);
  PostOrder post_search(generic x);

 protected:
  BTN<generic>* p_insert(generic x);                                           // returns a pointer to the new node
  BTN<generic>* p_remove(generic x);                                           // returns a pointer to the parent of the deleted node 
  BTN<generic>* p_search(generic x);                                           // returns a pointer to where x is found

 private:
  BTN<generic>* kill(BTN<generic>* x);
  using BT<generic>::m_size;
  using BT<generic>::m_root;
};


1
2
3
4
5
6
7
8
9
10
11
12
13
template<typename generic>
class AVL : public BST<generic>
{
 public:
  void insert(generic x);
  void remove(generic x);
  int getBalance(BTN<generic>* x);
  void negPosRotation(BTN<generic>* x);
  void negNegRotation(BTN<generic>* x);
  void posPosRotation(BTN<generic>* x);
  void posNegRotation(BTN<generic>* x);
  void setDepth(BTN<generic>* x);
};
~BT();

Do you have a definition for this destructor in the relevant .cpp file?
Last edited on
Yes my teacher gave us a .a file that has all the functions for BT and BST. But that must be the problem though because I don't see what else could be.
Yeah, sounds like it's missing that, go ask him.
It's too late for that. It is due at 4 AM. But he said it won't compile when we submit it because of the way we have to submit it. Hopefully my code is right without having to test it so I can get a decent grade on it.
Actually...I just realized you might need to link it...try using -l(nameoftheafile).a onto your command line (you are using g++ right?)
Yes he gave us the compile line to use so it should be working.
Topic archived. No new replies allowed.