Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.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/5/actionscript-3/6.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++_Red Black Tree_Template Classes - Fatal编程技术网

C++ 如何调整c++;使用模板类的代码

C++ 如何调整c++;使用模板类的代码,c++,red-black-tree,template-classes,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 =

以下代码是红黑树程序的一部分,该程序必须将
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
纽特水电站

#pragma once

template <class T>
class Knote {
public:
    Knote(T data = 0);
    bool rot;
    T item;
    Knote *links;
    Knote *rechts;
    Knote *eltern;
};
#pragma一次
模板
类结{
公众:
Knote(T数据=0);
布尔腐烂;
T项;
Knote*链接;
Knote*rechts;
纽特*埃尔滕;
};
纽特

#include "Knote.hpp"

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

    eltern = nullptr;
    links = nullptr;
    rechts = nullptr;
    rot = true;
}
#包括“Knote.hpp”
Knote::Knote(整数数据)
{
此->项目=数据;
eltern=nullptr;
links=nullptr;
rechts=nullptr;
rot=正确;
}
接下来我该怎么做呢

巴姆水电站

#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);
};
#pragma一次
#包括“Knote.hpp”
#包括
类鲍姆
{
公众:
Baum();
无效einfuegen(整数x);
无效ausgabe_levelorder();
无效ausgabe_顺序();
私人:
纽特*头;
void rs_einfuegen(Knote*&Knote,Knote*&eltern,int x,布尔西南);
内特罗特(纽特*&纽特);
无效链接\u旋转(节点*&链接\u节点);
无效记录旋转(节点*&链接节点);
无效级别顺序(Knote*Knote,标准::vector&KnoteEqueue,int niveau,标准::vector&niveauQueue);
无效排序_-levelorder(标准::向量和节点队列,标准::向量和niveauQueue);
无效顺序(Knote*Knote);
};
鲍姆

#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);
    }
}
#包括“Baum.hpp”
#包括
使用名称空间std;
鲍姆::鲍姆()
{
...
}
//XXX
void Baum::einfuegen(int x)
{
...
}
//XXX
内特鲍姆::罗特(纽特*&纽特)
{
...
}
//XXX
void Baum::rs_einfuegen(Knote*&Knote,Knote*&埃尔滕,int x,布尔西南)
{
...
}
//XXX
void Baum::links\u旋转(Knote*&links\u Knote)
{
...
}
//XXX
void Baum::rechts_旋转(Knote*&rechts_Knote)
{
...
}
//XXX
void Baum::ausgabe_levelorder()
{
...
}
//XXX
void Baum::levelorder(Knote*Knote、vector和knoteQueue、int-niveau、vector和niveauQueue)
{
...
}
//XXX
void Baum::sort_levelorder(向量和节点队列、向量和niveauQueue)
{
...
}
//XXX
void Baum::ausgabe_inoorder()
{
顺序(头->记录);
cout链接);
cout项目记录);
}
}
  • 不需要在课堂上使用
    Knote
    。只需使用
    Knote
    。而不是

    Knote<T> *links;
    Knote<T> *rechts;
    Knote<T> *eltern;
    
  • 使用类模板时,请确保提供模板参数

    Knote* head;
    
    这是不对的。你需要使用

    Knote<int>* head;
    
    Knote*头;
    

    Knote*头;
    
    您可以选择适合于
    Baum
    的类型

  • Knote
    的实现从.cpp文件移动到.h文件。看


  • 对于Knote.h,您的
    模板
    行应该是
    模板

    此外,在Knote的构造函数中,不能将int(数据)赋值给t变量(项)。对于构造函数,您应该使用
    T data
    ,而不是
    int data
    ,因为您不知道数据需要什么类型(因为它是一个模板)

    模板化类也没有cpp文件。实现必须在类声明之后进入.h(除非声明了forward)。如果您确实想分离头和“实现”代码部分,请保持.h正常,但为您的方法实现创建一个.hpp文件。在类声明后的.h中,放入
    #include“Knote.hpp”

    对于常规方法,格式如下所示:

    template <typename T>
    void Knote<T>::myMethod(parameters)
    {
        //normal method stuff
    }
    
    模板
    void Knote::myMethod(参数)
    {
    //常规方法材料
    }
    

    对于将模板类作为参数的友元方法,例如重载的插入运算符(首先,你不应该将模板实现与声明分离。关于这一点,有很多链接。例如:@PaulMcKenzie:除非有比你更权威的人说你必须对客户端隐藏所有实现细节:-(我照你说的做了,但是我得到了这个:
    错误C2512:'Knote':没有合适的默认构造函数可用
    @user300725,这是令人惊讶的。你有一个构造函数
    Knote(int data=0);
    应该用作默认构造函数。我将它改为
    Knote(T data)
    ,然后改为
    Knote(T data=0)
    代码编译成功!但是我得到字符的ASCII码作为输出,我需要字符本身…如何摆脱ASCII码?@user300725,请发布用于打印数据的函数。我想保留带有.hpp标题的单独表单…我可以为
    类型分配
    int
    >char
    type。如果我根本不使用模板类,而将
    char
    放在
    item
    而不是
    int
    ,我将有
    char
    输出,根据你的
    模板
    行应该是
    模板
    ,两者都是有效的。一些人喜欢使用
    typename
    ,而另一些人喜欢使用
    class
    @user300725是的,您可以通过类型升级将
    int
    分配给
    char
    。但是由于项的类型为T,它可以是任何东西,甚至是对象(尽管您没有将其与它们一起使用),因此类型升级并不总是有效。
    Knote<char>* head;
    
    template <typename T>
    void Knote<T>::myMethod(parameters)
    {
        //normal method stuff
    }
    
    //in class declaration in .h
    template <class T>
    class Bob
    {
        //variables here
    
        template <typename U>
        void myfunc(Bob<U> value);  //have to use a different template variable
    
    }
    
    //define as normal in the .hpp (or further down the file if no .hpp used)