如何初始化使用其他模板类的模板类成员
How do you initialize template class members that uses other template classes?
我在正确设置和访问类的成员函数时遇到了问题。此节点类用于构建最大堆树。然而,当树被初始化时,我得到的是垃圾数据,而不是我初始化它的数据。
#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>
的实例。
相关文章:
- 保留对其他类的成员函数的引用
- 成员包含其他继承类的继承类?
- 线程消息传递或更好:在"大师班"中访问其他班级的成员
- 锁定来自其他线程的类成员
- 为什么我可以访问其他班级的私人成员?
- C++:从其他类访问结构的私有成员向量
- 将基类的成员函数重载到其他派生类C++
- 用于对项目进行分组并将单个项目映射到其他组成员的数据结构
- 只对向量中的一个类成员进行排序,其他成员保持不变
- 成员函数不能为集合迭代器和const_iterator的输入重载(但可以为其他 STL 迭代器重载)
- 使用其他成员初始化结构的成员?
- 其他类的成员函数对成员函数的嵌套引用
- C++ 将类成员函数传递给其他类
- 访问其他类中多个对象的私有成员向量
- 对 Eclipse 项目库的更改将还原给团队的其他成员
- 其他成员函数的通用"成员函数"包装器?
- 在另一个 QThread 上运行成员方法时,无法将事件发送到其他线程拥有的对象
- 如果类包含基类类型的成员作为第一个元素,后跟其他成员,编译器是否可以优化空基?
- 将成员函数作为参数传递给其他成员函数 (C++ 11 <function>)
- 将元组成员包装在其他模板化类型中