【发布时间】:2013-12-28 19:44:40
【问题描述】:
我正在尝试使用模板实现红黑树。 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.
}
}
另外,还有什么想法可以请教吗?我不知道要通过什么。
【问题讨论】:
-
typedef 是不必要的。只需定义,例如,
struct tree data { int data; };