【发布时间】:2016-07-04 06:07:40
【问题描述】:
我无法正确设置和访问类的成员函数。该节点类用于构建最大堆树。但是,当树被初始化时,我得到的是垃圾数据,而不是我初始化它的内容。
#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
【问题讨论】:
-
请注意,array 是一个指针,因此 sizeof(array) 将返回一个常量(4 或 8,取决于是否编译为 64 位),而与 n 无关。你也应该在 ~MaxHeapTree() 中删除 [] 数组。
-
在您的
MaxHeapTree<>构造函数中,这一行:HeapNode<Type> *array = new HeapNode<Type>[n];是问题所在。您不是在初始化名为array的MaxHeapTree<>数据成员,而是在初始化一个新的局部变量。此外,每次构造实例时都会泄漏内存。 -
@user2296177 这如何解释为什么 elementSize、height 和 leafCounter 除了数组之外还提供垃圾数据?
-
我注意到您包含了
<array>,但没有使用std::array? -
@XerenNarcy 是的,我认为我需要它使用 sizeof(array)
标签: c++ dynamic-arrays template-classes