Hey everyone,
I just started writing this program when I hit a problem with the destructor. Me and my buddies in the lab could not figure out what is going on. The error said something about symbol referencing error and refers to the destructor. The program is set in three files as a driver, header, and implementation.

// Main File
#include <iostream>
#include "tree.h"
using namespace std;

main ()
{

fktree directory_tree;
directory_tree.build_tree();
}



//  Header file
#ifndef TREE_H

#include <string.h>
#include <iostream>
using namespace std;

struct node
{
string info;
int size;
node * firstkid
node * nextbro
};


class fktree
{
public:
fktree();
~fktree();

void build_tree();

private:

node *root, *curr_pos;

};

#define TREE_H
#endif



// Implementation File
#include <string.h>
#include <iostream>
#include "tree.h"
using namespace std;


fktree::fktree()
{
root = new node;
root->info = "/";
root->size = 1;
root->firstkid = NULL;
root ->nextbro = NULL;
}


void fktree::build_tree()
{

}
Comments
Use code tags.

Huh, I was under the impression that the destructor was basically a C++ function where ~(constructor) would automatically delete everything to do with the class. I didn't know I had to write that myself. You learn something new everyday.

>I didn't know I had to write that myself.
You only need to write your own destructor if the default destructor doesn't do what you want. For example, if you have any dynamically allocated memory in your object then you need to write your own destructor. But remember that if you declare the destructor, you must define it as well. That was your problem.

Ok, so the destructor would look something like this.

// Implementation File
#include <string.h>
#include <iostream>
#include "tree.h"
using namespace std;


fktree::fktree()
{
root = new node;
root->info = "/";
root->size = 1;
root->firstkid = NULL;
root ->nextbro = NULL;
}

fktree::~fktree()
{
  // Recursive function to delete tree.

}
void fktree::build_tree()
{

}

So the destructor will automatically destroy any variable declared in the class and I will write the function to delete all the nodes of the tree. What about other variables I declared in various functions? Will those be deleted since they were not dynamically allocated or do I still need to do those?

This article has been dead for over six months. Start a new discussion instead.