我正在尝试使用模板来实现一棵红黑树。insert函数采用两种通用类型,即Item和Key。但是,当我在main()中创建RedBlackTree的实例并调用函数“ InsertKey”时,程序给出错误:无法解析方法“ InsertKey”。另外,我不知道在“ InsertKey”函数中作为参数传递什么。我实现了一个由随机元素组成的数组。数组应该是参数之一,但是我不知道另一个参数是什么。
这是我的头文件:
#ifndef REDBLACKTREE_H_
#define REDBLACKTREE_H_
template <class Item, class Key>
class RedBlackTree
{
typedef enum
{
BLACK,
RED
}ColourNode;
/* user data stored in tree */
typedef struct {
int data;
} treedata;
typedef struct RBT
{
struct RBT *left;
struct RBT *right;
struct RBT *parent;
struct RBT *root;
ColourNode colour;
//Item item;
Key key;
treedata data;
}RBTNode;
public:
~RedBlackTree(); // destructor
RedBlackTree(Item, Key); // default constructor
void InsertKey(const Item *&, const Key *&);
void FixingInsert(const Item *&, const Key *&);
int RemoveKey(Item, Key);
int FindKey(Item, Key);
//private:
//RedBlackTree<Item, Key> *rootPointer;
};
#endif /* REDBLACKTREE_H_ */
这是我的main()
#include <iostream>
#include <string>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include "RedBlackTree.h"
using namespace std;
int main(int argc, const char* argv[])
{
const int arraysize = 200;
int arr[arraysize];
RedBlackTree<int, int> t1(int, int);
srand((unsigned)time(0));
for(int i = 0; i <= arraysize-1; i++)
{
arr[i] = rand() % 210;
//printf("%d ", arr[i]);
}
for(int i = 0; i <= arraysize-1; i++)
{
t1.InsertKey(arr[i], // something else//); //InsertKey should have another parameter, but for now I am trying to figure out why it cannot be resolved.
}
}
另外,对其他东西可能有什么想法吗?我不知道该怎么办。
RedBlackTree<int, int> t1(int, int);
在这里,您要声明一个函数t1
,该函数返回aRedBlackTree<int, int>
并接受两个均为type的参数int
。
我认为您实际上是想创建一个RedBlackTree<int, int>
对象并调用它t1
。它有一个构造函数,需要一个Item
和一个Key
。但是,您已将其评论为是默认构造函数,而事实并非如此。默认构造函数是不带任何参数的构造函数。我认为您的意思是这样声明构造函数:
RedBlackTree(); // default constructor
然后,您可以创建这种类型的对象,如下所示:
RedBlackTree<int, int> t1;
您永远不会在普通的括号之间传递类型(int, int)
。它们总是放在尖括号之间,例如<int, int>
和在模板名称之后。在这种情况下,模板名称为RedBlackTree
,我们想使用int
类型实例化它,所以我们这样做RedBlackTree<int, int>
。没有什么要传递给构造函数的。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句