0

クラスのメンバー関数を正しく設定してアクセスするのに問題があります。このノード クラスは、Max Heap Tree を構築するために使用されています。ただし、ツリーが初期化されているとき、初期化しているものではなく、ガベージデータを取得しています。

#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

これが、ヒープ ツリーを構築するクラスです。コンストラクターであらゆる方法で初期化を設定しようとしましたが、まだジャンク データを取得しています。さらに、整数を取るようにコンストラクターを設定しましたが、ドライバー プログラムでツリーを作成するときに、そのサイズの配列を開始する引数を配置できません。

#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
4

1 に答える 1