Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/160.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用C+中的模板和错误+;_C++_Tree - Fatal编程技术网

C++ 使用C+中的模板和错误+;

C++ 使用C+中的模板和错误+;,c++,tree,C++,Tree,我正在尝试使用模板实现一个红黑树。例如,将项插入到树中时,键和项都应为泛型类型。到目前为止,我实现了一个头文件,它由一个结构和要实现的函数组成。但是,我不知道我是否以正确的方式使用模板。另外,当我尝试实现“插入”功能时,IDE给出了错误: “void RedBlackTree::InsertKey(Item*&,Key*&)”的原型与类“RedBlackTree”RedBlackTree.h中的任何原型都不匹配 这是我的头文件: #ifndef REDBLACKTREE_H_ #define R

我正在尝试使用模板实现一个红黑树。例如,将项插入到树中时,键和项都应为泛型类型。到目前为止,我实现了一个头文件,它由一个结构和要实现的函数组成。但是,我不知道我是否以正确的方式使用模板。另外,当我尝试实现“插入”功能时,IDE给出了错误: “void RedBlackTree::InsertKey(Item*&,Key*&)”的原型与类“RedBlackTree”RedBlackTree.h中的任何原型都不匹配

这是我的头文件:

#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_ */
\ifndef REDBLACKTREE\u H_
#定义红黑树_
模板
类红黑树
{
类型定义枚举
{
黑色
红色
}染色结节;
类型定义结构RBT
{
结构RBT*左;
结构RBT*右;
结构RBT*父级;
结构RBT*根;
颜色节点颜色;
项目;
钥匙;
}RBTNode;
公众:
~RedBlackTree();//析构函数
RedBlackTree(项,键);//默认构造函数
无效插入键(项,键);
int InsertFixUp(项,键);
int RemoveKey(项,键);
int FindKey(项目、关键);
私人:
红黑树*根指针;
红黑树*NILL_叶;
};
模板
void RedBlackTree::InsertKey(项*&T,项*&z)
{
//节点*nil=树->nil;
//节点*根=树->根;
RBTNode*y;
RBTNode*x;
y=T->nil;
x=T->root;
而(x!=T->nil)
{
y=x;
如果((z->键)键))
x=x->左;
其他的
x=x->右;
}
y=z->父级;
如果(y==T->nil)
z=T->root;
其他的
如果((z->键)键))
z=y->左;
其他的
z=y->右;
z->left=T->nil;
z->right=T->nil;
z->颜色=红色;
插入固定(T,z);
}
#endif/*REDBLACKTREE_H_*/

提前感谢。

问题是
InsertKey
的参数类型与声明不匹配。在声明中,参数是
Item
Key
,在实现中,参数是
Item*&
Key*&
(对指针的引用)。这些需要匹配

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

您必须将函数的实现(模板)移动到类定义中

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

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

    //...
};
模板
类红黑树
{
//...
公众:
~RedBlackTree();//析构函数
RedBlackTree(项,键);//默认构造函数
无效插入键(项*&T,项*&z)
{
//...
}
//...
};

是的,这是使用模板的正确方法。您的InsertKey原型确实不匹配。一个有
Item
和另一个
Item*&
,这是两种截然不同的类型。我解决了这个问题。然而,当我尝试使用您的方式时,出现了很多错误—我认为
InsertKey()
使用了其他类型的名称。我试图在我的主函数中创建一个RedBlackTree的实例,但它不接受:RedBlackTree t1;我解决了它,但实例没有找到我的函数“InsertKey”:t1.InsertKey(arr[I])@user3136756该函数接受两个参数。您只给出了一个。是的,我知道,但实例t1甚至找不到方法“InsertKey”,因为它显示了错误:无法解析方法“InsertKey”