c++ - 如何调整 C++ 代码以使用模板类

标签 c++ red-black-tree template-classes

以下代码是红黑树程序的一部分,它必须将 item 作为 char 或 int,所以我决定使用模板类,但我不知道如何扩展它通过完整的程序,编译器向我发送了数千个错误:

代码中有德文名称,如果方便理解,我会翻译其中的一些:

baum = tree
knote = node
links = left
rechts = right
rot = red
doppel = double
mittlere = middle
eltern = parent
einfuegen = insert
rs = rb = red black

Knote.hpp

#pragma once

template <class T>
class Knote {
public:
    Knote(T data = 0);
    bool rot;
    T item;
    Knote *links;
    Knote *rechts;
    Knote *eltern;
};

Knote.cpp

#include "Knote.hpp"

Knote<int>::Knote(int data)
{
    this->item = data;

    eltern = nullptr;
    links = nullptr;
    rechts = nullptr;
    rot = true;
}

剩下的我该怎么办?

鲍姆.hpp

#pragma once

#include "Knote.hpp"

#include <vector>

class Baum
{
public:
    Baum();
    void einfuegen(int x);
    void ausgabe_levelorder();
    void ausgabe_inorder();
private:
    Knote<int>* head;
    void rs_einfuegen(Knote<int>* &knote, Knote<int>* &eltern, int x, bool sw);
    int rot(Knote<int>* &knote);
    void links_rotation(Knote<int> * &links_knote);
    void rechts_rotation(Knote<int> * &links_knote);
    void levelorder(Knote<int>* knote, std::vector<Knote<int>*> &knoteQueue, int niveau, std::vector<int> &niveauQueue);
    void sort_levelorder(std::vector<Knote<int>*> &knoteQueue, std::vector<int> &niveauQueue);
    void inorder(Knote<int>* knote);
};

鲍姆.cpp

#include "Baum.hpp"

#include <iostream>

using namespace std;

Baum::Baum()
{
    ...
}

// XXX
void Baum::einfuegen(int x)
{
    ...
}

// XXX
int Baum::rot(Knote<int>* &knote)
{
    ...
}

// XXX
void Baum::rs_einfuegen(Knote<int> *& knote, Knote<int> *&eltern, int x, bool sw)
{
    ...
}

// XXX
void Baum::links_rotation(Knote<int>* &links_knote)
{
    ...
}

// XXX
void Baum::rechts_rotation(Knote<int>* &rechts_knote)
{
    ...
}

// XXX
void Baum::ausgabe_levelorder()
{
    ...
}

// XXX
void Baum::levelorder(Knote<int>* knote, vector<Knote<int>*> &knoteQueue, int niveau, vector<int> &niveauQueue)
{
    ...
}

// XXX
void Baum::sort_levelorder(vector<Knote<int>*> &knoteQueue, vector<int> &niveauQueue)
{
    ...
}

// XXX
void Baum::ausgabe_inorder()
{
    inorder(head->rechts);
    cout << endl;
}

// XXX
void Baum::inorder(Knote<int>* knote)
{
    if (knote != nullptr)
    {
        inorder(knote->links);
        cout << knote->item << " ";
        inorder(knote->rechts);
    }
}

最佳答案

  1. 不需要使用Knote<T>在类里。只需使用 Knote .而不是

    Knote<T> *links;
    Knote<T> *rechts;
    Knote<T> *eltern;
    

    只需使用:

    Knote *links;
    Knote *rechts;
    Knote *eltern;
    
  2. 使用类模板时,确保提供模板参数。

    Knote* head;
    

    是不对的。你需要使用

    Knote<int>* head;
    

    Knote<char>* head;
    

    您选择适合 Baum 的类型.

  3. 移动Knote的实现从 .cpp 文件到 .h 文件。参见 Why can templates only be implemented in the header file? .

关于c++ - 如何调整 C++ 代码以使用模板类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30381126/

相关文章:

c++ - 通过 operator[] 访问静态成员 unordered_map

algorithm - 红黑树 : I am trying to understand deletion of nodes in redblack trees?

data-structures - 红黑树与B树

c++ - C++ 中的回调定时器函数

c++ - 我可以使用非模板类专门化可变参数模板模板参数吗?

algorithm - 如何用红黑树实现多重集?

c++ - 专用功能拉开功能型

c++ - 如何创建仅包含主文件和模板 header 的 makefile (C++)

c++ - 当我将带有非模板类的模板类放在同一个 cpp 文件中时出现链接错误 - C++

C++ const 引用允许从表达式更改?