如何初始化使用其他模板类的模板类成员

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

本文关键字:成员 其他 初始化      更新时间:2023-10-16

我在正确设置和访问类的成员函数时遇到了问题。此节点类用于构建最大堆树。然而,当树被初始化时,我得到的是垃圾数据,而不是我初始化它的数据。

#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
如何初始化使用其他模板类的模板类成员?

用同样的方法初始化不使用其他模板的非模板的成员。

当树被初始化时,我得到的是垃圾数据,而不是我初始化它的数据。

我用的是MaxHeap<string> *heapTree1;

哦,这就是你的问题。显然,您从未创建MaxHeap<string>的实例。