【问题标题】:Using templates with C++在 C++ 中使用模板
【发布时间】: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; };

标签: c++ templates


【解决方案1】:
RedBlackTree<int, int> t1(int, int);

在这里,您声明了一个名为 t1 的函数,它返回一个 RedBlackTree&lt;int, int&gt; 并接受两个 int 类型的参数。

我认为您实际上想创建一个RedBlackTree&lt;int, int&gt; 对象并将其命名为t1。它有一个构造函数,它接受一个Item 和一个Key。但是,您已将其评论为说它是 default 构造函数,但事实并非如此。默认构造函数是不带参数的构造函数。我认为您的意思是像这样声明构造函数:

RedBlackTree(); // default constructor

然后您可以像这样创建这种类型的对象:

RedBlackTree<int, int> t1;

你永远不会在像(int, int) 这样的普通括号之间传递类型。它们总是在尖括号之间,如&lt;int, int&gt; 和模板名称之后。在这种情况下,模板名称是RedBlackTree,我们想用int 类型实例化它,所以我们使用RedBlackTree&lt;int, int&gt;。没有任何东西可以传递给构造函数。

【讨论】:

  • 非常感谢。您是否知道我可以在 InsertKey 函数中作为另一个参数传递什么,因为我不知道要传递什么才能在红黑树中添加元素?
【解决方案2】:

RedBlackTree&lt;int, int&gt; t1(int, int); 被视为函数声明。您还没有为 RedBlackTree 定义默认构造函数,因此您必须提供一个,或者使用参数构造您的对象。

RedBlackTree() {  }

下一个问题是 InsertKey 的函数签名需要两个参数,而你只给它一个。

void InsertKey(const Item *&, const Key *&);
t1.InsertKey(arr[i], /* something else must go here */)

除此之外,当您的函数需要 int *&amp; 时,您将传递 int。如果您打算通过 const-reference 传递这些参数,请删除星号。

void InsertKey(const Item &, const Key &);

否则,你需要传递一个指针。

t1.InsertKey(&arr[i], /* something else */);

问题是它需要一个const 指针,所以你必须这样做:

const int* pointer = &arr[i];
t1.InsertKey(pointer, /* something else */);

【讨论】:

  • 这既不是令人烦恼的解析,也不是最令人烦恼的解析。这只是一个直接的函数声明。
  • @sftrabbit 好的,我从答案中删除了那部分。
  • 谢谢。由于我不知道要通过什么来在红黑树中添加元素,所以请提出其他建议?
【解决方案3】:

你向用户承诺了一个函数InsertKey(),它接受两个指针。您尝试使用一个值调用InsertKey()。这不会飞。

作为一般建议,在将其转换为模板之前,请确保您的数据结构适用于具体类型(算法则相反)。我还建议您 RedBlackTree&lt;K, V&gt; 使用 KV 对象而不是使用指针。如果用户真的想使用指针,可以通过指定指针参数来使用类模板。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-08
    • 1970-01-01
    • 1970-01-01
    • 2014-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多