Menu

Constructor question - initially empty class

karmul
2008-09-21
2012-09-26
  • karmul

    karmul - 2008-09-21

    Hi,

    I want to construct an initially empty class and populate it later, when I receive values to add from an input file. My project currently compiles and runs; just not with expected results. There is something wrong with the way I am constructing the tree and /or the way I handle cases in the add_node function where the "root" parameter received by that function always evalutates as NULL.

    I have tried different variations of the constructor. In previous versions, when I started the tree in main.cpp with a value, the rest of the program's functions worked as expected - but my tree would never match the expected output because I had to start it with some arbitrary integer. Now, when I start with an initially empty tree (as I have the code snippets shown doing now), it never gets rid of the idea that the root is NULL. I'm not quite sure how to solve this.

    I have 3 files in my project: main.cpp, avltree.h, and avltree.cpp. I think I've included all relevant snippets from main.cpp and avltree.cpp.

    //********
    main.cpp
    //
    ********

    include <cstdio>

    include <cstdlib>

    include <iostream>

    include <fstream>

    include "avltree.h"

    using namespace std;
    using namespace Trees;

    int main(int argc, const char argv[])
    {
    //
    node
    root = new node();
    tree myTree(root);
    ...
    ...
    case 'A': //Attempt to add the following integer to the tree;
    value = atoi(&buffer[1]);
    myTree.add_node(root, value);
    break;
    ...
    return 0;
    }

    //********
    avltree.cpp
    //
    ********

    node::node(int x)
    {
    data = x;
    //the integer passed to the constructor becomes
    //the data in the node
    left = NULL;
    right = NULL;
    parent = NULL;

    }

    node::~node()
    {
    delete left;
    delete right;
    delete parent;
    }

    tree::tree()
    {
    root = NULL;
    right_tree = NULL;
    left_tree = NULL;
    count_lh = 0;
    count_equal = 1;
    count_rh = 0;
    }

    tree::tree(node *currptr)
    {
    root = currptr; //the pointer to the node used as the argument becomes
    //the root of the tree
    // std::cout << root->data << std::endl;
    // std::cout << "Constructing an empty tree." << std::endl;
    count_lh = 0;
    count_equal = 1;
    count_rh = 0;
    }

    void tree::add_node(node *currptr, int num)
    {
    if(currptr == NULL) //create the root node of the tree
    {
    //I expect currptr to be NULL
    //only the first time the add_node function is called
    //in actuality, each time this function is called
    //currptr is evaluating as NULL

        //I created this helper function to set the value of the
        //root, but it doesn't seem to do anything
    
        root-&gt;set_data(num);
    
        //I also tried other things here like:    
        //node * root = new node(num);
        taller = true;
        std::cout &lt;&lt; &quot;The value entered (&quot; &lt;&lt; num &lt;&lt; &quot;) was used as the root of the tree.&quot; &lt;&lt; std::endl;
    }
    
    else
    {
    //doesn't matter what happens here because this code    
    never executes!
    }
    

    }

     
    • cpns

      cpns - 2008-09-22

      > I want to construct an initially empty class and populate it later,

      You cannot have an empty class. You can have an ininitialised one, or one initialised to defaults that have no effect, but it cannot be 'empty'.

      > node* root = new node();

      How does that work when you have no default constructor? Or is it defined in-line in the header?

      Since you have drastically edited the code and not really provided a clear explanation of the problem it is hard to assist; but it looks to me like you only ever instantiate one node object and pass it to both the constructor and to add_node(). To be honest you have either edited this code so drastically that it no longer represents a tree class, or the code is nonsense.

      > //I expect currptr to be NULL

      Why do you expect that? You never pass it NULL, you pass it 'root' instantiated in main().

      Clifford

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.