簡體   English   中英

如何調整C ++代碼以與模板類一起使用

[英]how to adjust a c++ code to work with template class

以下代碼是Red Black Tree程序的一部分,該程序必須將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;
}

現在剩下的我該怎么辦?

Baum.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);
};

Baum.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文件。 請參閱為什么只能在頭文件中實現模板?

對於Knote.h, template <typename T>行應為template <class T>

另外,在Knote的構造函數中,您無法將int(數據)分配給T變量(項目)。 對於構造函數,您應該使用T data而不是int data ,因為您不知道數據的類型是什么(因為它是模板)。

模板化類也沒有cpp文件。 該實現必須在類聲明之后(除非正向聲明)之后放入.h中。 want to separate your header and "implementation" code parts, keep the .h as normal, but make an .hpp file for your method implementations. 如果要分離標題和“實現”代碼部分,請保持.h正常,但為方法實現創建一個.hpp文件。 在類聲明后的.h中,放入#include "Knote.hpp"

對於普通方法,格式如下:

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM