Recursive BST Destructor

Hey guys,

I'm trying to implement a binary search tree. I'm having an issue with the destructor, which calls another function to kick off the recursive deletion of all nodes.

Compiling with
g++ -std=c++14 -pedantic
on Ubuntu

I'm getting the following error:
/tmp/ccrumI5U.o: In function 'bst::~bst()':
bst.cpp:(.text+0x7c): undefined reference to 'bst::DESTROY(bstNode*)'
collect2: error: ld returned 1 exit status


Below are the destructor and DESTROY functions:
Destructor (public):
1
2
3
bst::~bst() {
	DESTROY(root);
} // END ~bst() 


DESTROY (private):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void DESTROY(bstNode* cur) {
	if(cur != nullptr) {

		if( (cur->left == nullptr) && (cur->right == nullptr) ) {
			delete cur;
			cur = nullptr;
		} else {
			DESTROY(cur->left);
			DESTROY(cur->right);
		} // END if/else

	} // END if

return;
} // END DESTROY(cur) 


Any advice? I have tried moving the DESTROY function to public scope and defining it before the destructor but the error persists...
Last edited on
Where did you implement the DESTROY function, in the class definition or outside the class definition?

This is the entire source code:

BST.h
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
#ifndef BST_H
#define BST_H

struct bstNode {
	bstNode* left;
	bstNode* right;
	int data;
};

#include <string>

using std::string;

class bst {
public:
	bst();
	bst(const string& toRead);
	~bst();

	void Read(const string& toRead);
	void Print();
	bool insert(int toAdd);
	int getNodeCount() const;
	
private:
	bstNode* root;
	unsigned nodeCount;
	bool insert(int toAdd, bstNode* cur);
	void Print(bstNode* cur);
	void DESTROY(bstNode* cur);
	

}; // END bst

#endif 


_________________________________________________________________________________

BST.cpp:
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
#include "bst.h"

#include <fstream>
#include <cassert>

#include <iostream>
using std::cout;

using std::ifstream;

bst::bst() : root(nullptr), nodeCount(0) {

} // END bst()

bst::bst(const string& toRead) : root(nullptr), nodeCount(0) {
	Read(toRead);
} // END bst(toRead)

bst::~bst() {
	DESTROY(root);
} // END ~bst()

void bst::Read(const string& toRead) {
	ifstream fin(toRead);
	assert(fin);

	int temp;
	while(!fin.eof() && (fin >> temp)) {
		insert(temp);
	} // END while(!fin.eof() && (fin >> temp))

} // END Read(toRead)

void bst::Print() {
	Print(root);
} // END Print()

void bst::Print(bstNode* cur) {
	if(cur != nullptr) {
		Print(cur->left);
		cout << cur->data;
		Print(cur->right);
	} // END if
} // END Print(cur)

void DESTROY(bstNode* cur) {
	if(cur != nullptr) {

		if( (cur->left == nullptr) && (cur->right == nullptr) ) {
			delete cur;
			cur = nullptr;
		} else {
			DESTROY(cur->left);
			DESTROY(cur->right);
		} // END if/else

	} // END if

return;
} // END DESTROY(cur)

bool bst::insert(int toAdd) {
	insert(toAdd, root);
} // END insert(toAdd)

bool bst::insert(int toAdd, bstNode* cur) {
	if(cur == nullptr) {
		std::cout << toAdd << '\n';

		cur = new bstNode;
		cur->data = toAdd;

		nodeCount++;
	} else if(toAdd < cur->data) {
		insert(toAdd, cur->left);
	} else {
		insert(toAdd, cur->right);
	} // END if/else

} // END insert(toAdd, cur)

int bst::getNodeCount() const {
	return nodeCount;
} // END getNodeCount() 


I've tried moving the definition/declaration between public/private and before/after the destructors but it doesn't seem to change anything.
WOW. Missing the scope resolution for the DESTROY function. LIne 46 of bst.cpp should be void bst::DESTROY... instead of void DESTROY... Been looking at this for an hour and a half. *sigh*
Topic archived. No new replies allowed.