0

I'm having trouble correctly setting up and accessing my member functions of a class. This node class is being used to build a Max Heap Tree. However, when the tree is being initialized, I'm getting garbage data and not what I am initializing it to.

#ifndef HEAPNODE_H_INCLUDED
#define HEAPNODE_H_INCLUDED

#include <iostream>
#include <cstdlib>
#include <array>
using namespace std;

template <class Type> class HeapNode {
private:
    int key;
    Type value;
public:
    HeapNode(int key, Type const &value) {
        this->key = key;
        this->value = value;
    }

    // Returns the key of the node
    int getKey() {
        return key;
    }

    // Returns the value of the node
    Type getValue() {
        return value;
    }

    // Displays the node
    void displayNode() {
        cout << "Key: " << key << "\tValue: " << value << endl;
    }
};

#endif

Here is the class that builds my Heap Tree. I've tried setting the initializations in the constructor every which way, and I'm still getting junk data. In addition, I set the constructor to take an integer, but when I'm creating a tree in my driver program, it won't let me put an argument for it which initiates an array of that size.

#ifndef MAXHEAPTREE_H_tINCLUDED
#define MAXHEAPTREE_H_INCLUDED

#include <iostream>
#include <cstdlib>
#include <fstream>
#include <string>
#include "HeapNode.h"
using namespace std;

template <class Type> class MaxHeapTree {
private:
    HeapNode<Type> *array;
    HeapNode<Type> *root;
    int elementSize;
    int height;
    int leafCounter;
public: 
    // Constructor
    MaxHeapTree(int n = 10) : elementSize(0), height(0), leafCounter(0) {
        this->elementSize = elementSize;
        this->height = height;
        this->leafCounter = leafCounter;
        HeapNode<Type> *array = new HeapNode<Type>[n];
    }

    // Destructor
    ~MaxHeapTree();

    void arrayDisplay() {
        cout << "Original array size: " << sizeof(array)/4 << endl;
    }

    // Returns the number of elements in the tree
    int getSize() {
        return elementSize;
    }

    // Returns the height of the tree
    int getHeight() {
        return height;
    }

    // Returns the number of leaves in the tree
    int leaves() {
        return leafCounter;
    }

    int countLines(const string fileName) {
        string line;
        int lineCount = 0;

        ifstream myFile (fileName.c_str());
        if (myFile.is_open()) {
            while (getline(myFile, line)) {
                lineCount++;
            }
        }
        else {
            cout << "Error opening file" << endl;
        }
        myFile.close();
        return lineCount;
    }

    // Reads structure from a text file and builds a max heap
    void buildTree(const string fileName) {
        string line;
        string key;
        string value;
        int lines = countLines(fileName);
        int i = 0;
        cout << "Lines: " << lines << endl;
        HeapNode<Type> *newArray[lines];
        cout << "Size of newArray: " << sizeof(newArray)/4 << endl;

        ifstream myFile (fileName.c_str());
        if (myFile.is_open()) {
            while (getline(myFile, line)) {
                key = line.substr(0, 1);
                int x = atoi(key.c_str());
                value = line.substr(1);

                HeapNode<Type> *hNode = new HeapNode<Type>(x, value);

                newArray[i] = hNode;
                cout << "newArray[" << i << "] = ";
                newArray[i]->displayNode();
                i++;
            }
        }
        else {
            cout << "2 - Error opening file." << endl;
        }
        myFile.close();
    }
};

#endif
luigi741
  • 5
  • 1
  • 1
    Note that array is a pointer, so sizeof(array) will return a constant (4 or 8 depending on whether compiled to 64 bit) regardless of n. Also you should delete[] array in ~MaxHeapTree(). – stewbasic Jul 04 '16 at 06:19
  • In your `MaxHeapTree<>` constructor, this line: `HeapNode *array = new HeapNode[n];` is the issue. You are not initializing the `MaxHeapTree<>` data member called `array`, you're initializing a new local variable. Furthermore, you leak memory every time an instance is constructed. – user2296177 Jul 04 '16 at 06:44
  • @user2296177 How would that explain why elementSize, height, and leafCounter are giving junk data though, in addition to array? – luigi741 Jul 04 '16 at 06:45
  • I notice you're including `` but not using `std::array`? – Xeren Narcy Jul 04 '16 at 06:50
  • @XerenNarcy Yes, I thought that I needed it use sizeof(array) – luigi741 Jul 04 '16 at 06:52
  • I don't even know how you're able to instantiate your class since your destructor isn't even defined. Are you using `MaxHeapTree<> x = new MaxHeapTree<>;`? – user2296177 Jul 04 '16 at 06:55
  • @user2296177 I was using `MaxHeap *heapTree1;` I just changed it to `MaxHeapTree *heapTree1 = new MaxHeapTree(12);` And now it initializes those elements to 0 – luigi741 Jul 04 '16 at 06:59
  • That doesn't create an instance of your heap tree, that simply creates a uninitialized pointer to one... since the pointer is uninitialized, it points to a random memory location which you are accessing and invoking undefined behaviour. Please get a book from here:http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list. You have many more issues with your code. – user2296177 Jul 04 '16 at 07:00

1 Answers1

0

How do you initialize template class members that uses other template classes?

In the same way you initialize members of non templates that don't use other templates.

when the tree is being initialized, I'm getting garbage data and not what I am initializing it to.

I was using MaxHeap<string> *heapTree1;

Well, there's your problem. Apparently you never created an instance of MaxHeap<string>.

Community
  • 1
  • 1
eerorika
  • 232,697
  • 12
  • 197
  • 326