954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Destructor help

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()
{

}
jasweb2002
Junior Poster in Training
56 posts since Sep 2004
Reputation Points: 11
Solved Threads: 2
 

Did you implement a destructor? You didn't list one above. That could be the problem!

Chainsaw
Posting Pro in Training
436 posts since Jun 2004
Reputation Points: 36
Solved Threads: 11
 

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.

jasweb2002
Junior Poster in Training
56 posts since Sep 2004
Reputation Points: 11
Solved Threads: 2
 

>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.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

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?

jasweb2002
Junior Poster in Training
56 posts since Sep 2004
Reputation Points: 11
Solved Threads: 2
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You