简体   繁体   English

在C ++中使用模板和错误

[英]Working with templates and error in C++

I am trying to implement a red black tree with the use of templates. 我正在尝试使用模板来实现一棵红黑树。 For example, when inserting an item to the tree, the key and the item should both be generic types. 例如,当将一个项目插入树时,键和该项目都应为通用类型。 Till now, I implemented a header file which consists of a struct and functions to be implemented. 到目前为止,我实现了一个头文件,该头文件由要实现的结构和函数组成。 However, I don't know if I'm using templates the right way. 但是,我不知道我是否以正确的方式使用模板。 Also, when I tried to implement the 'Insert' function, the IDE gives the error: prototype for 'void RedBlackTree::InsertKey(Item*&, Key*&)' does not match any in class 'RedBlackTree' RedBlackTree.h 另外,当我尝试实现'Insert'函数时,IDE给出错误:'void RedBlackTree :: InsertKey(Item *&,Key *&)'的原型与类'RedBlackTree'RedBlackTree.h中的任何内容都不匹配。

This is my header file: 这是我的头文件:

#ifndef REDBLACKTREE_H_
#define REDBLACKTREE_H_

template <class Item, class Key>
class RedBlackTree
{
    typedef enum
    {
        BLACK,
        RED
    }ColourNode;

    typedef struct RBT
    {
        struct RBT *left;
        struct RBT *right;
        struct RBT *parent;
        struct RBT *root;
        ColourNode colour;
        Item item;
        Key key;
    }RBTNode;

    public:
        ~RedBlackTree(); // destructor
        RedBlackTree(Item, Key); // default constructor

        void InsertKey(Item, Key);
        int InsertFixUp(Item, Key);
        int RemoveKey(Item, Key);
        int FindKey(Item, Key);

    private:
        RedBlackTree<Item, Key> *rootPointer;
        RedBlackTree<Item, Key> *NILL_LEAF;

};

template <class Item, class Key>
void RedBlackTree<Item, Key>::InsertKey(Item *&T, Key *&z)
{
    //node* nil=tree->nil;
    //node* root=tree->root;
    RBTNode *y;
    RBTNode *x;
    y=T->nil;
    x=T->root;

    while(x != T->nil)
    {
        y=x;
        if((z->key)<(x->key))
            x=x->left;
        else
            x=x->right;
    }

    y=z->parent;

    if(y == T->nil)
        z=T->root;
    else
    if((z->key)<(y->key))
        z=y->left;
    else
        z=y->right;
        z->left=T->nil;
        z->right=T->nil;
        z->colour=RED;
        InsertFixUp(T,z);
}
#endif /* REDBLACKTREE_H_ */

Thanks in advance. 提前致谢。

The problem is that the types of the arguments to InsertKey don't match the declaration. 问题是InsertKey的参数类型与声明不匹配。 In the declaration the arguments are Item and Key , and in the implementation they are Item*& and Key*& (references to pointers). 在声明中,参数为ItemKey ,在实现中,参数为Item*&Key*& (对指针的引用)。 These need to match. 这些需要匹配。

void InsertKey(Item, Key);
               ^^^^  ^^^
void RedBlackTree<Item, Key>::InsertKey(Item *&T, Key *&z)
                                        ^^^^^^^   ^^^^^^

You have to move the implementation of the function (the template) to the class definition. 您必须将函数(模板)的实现移至类定义。

template <class Item, class Key>
class RedBlackTree
{
//...
public:
    ~RedBlackTree(); // destructor
    RedBlackTree(Item, Key); // default constructor

    void InsertKey(Item *&T, Key *&z)
    {
        //...
    }

    //...
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM