简体   繁体   English

在 C++ 中模板化列表

[英]Templating a List in C++

I'm writing a program that needs a SkipList that I wrote a while back, but now I need it templated because it needs to use a custom class that I created.我正在编写一个程序,它需要一个我不久前写的 SkipList,但现在我需要模板化它,因为它需要使用我创建的自定义 class。 The problem I have is with creating the node in the List class. I use template <class t> on all my methods but I think that since both the node and the SkipList class use template <class t> , the same class t s interfere with each other or something.我遇到的问题是在列表 class 中创建节点。我在所有方法上都使用template <class t>但我认为由于节点和 SkipList class 使用template <class t> ,相同的class t会干扰彼此什么的。

In my SkipList.h, I have the method saying在我的 SkipList.h 中,我有方法说

Node <t>* createnode<t>(t,int);

And in the SkipList.cpp, the method says在 SkipList.cpp 中,该方法说

template <class t>
Node<t>* SkipList<t>::createnode(t value, int level) {
    Node<t> *n = new Node<t>(value, level);
    return n;
}

This gives me an error in the.h file that says Template specialization requires 'template<>' , and when I add it in, it replaces the code to say这在 .h 文件中给出了一个错误,提示Template specialization requires 'template<>' ,当我添加它时,它替换了代码

template<> Node<t>* createnode<t>(t,int);

But then my.cpp file says that there is no function definition anymore.但是然后 my.cpp 文件说不再有 function 定义。

Does anyone know where I'm going wrong or what I'm missing?有谁知道我哪里出错了或者我错过了什么? Thanks a ton for the help.非常感谢您的帮助。 My GitHub is here if anyone needs it for clarification如果有人需要澄清,我的 GitHub 在这里

Few things to note:需要注意的几点:

Now to address some things in the code.现在解决代码中的一些问题。 I am not sure if template <class t, class t> is valid, but at best it is useless in this case.我不确定template <class t, class t>是否有效,但充其量在这种情况下它是无用的。 Addressing your question: Yes the two class t s interfere with each other, but you don't need two different t s.解决您的问题:是的,两个class t会相互干扰,但您不需要两个不同的t You can't create a Node<t> with a t not matching the type of the List can you?您不能创建一个tList类型不匹配的Node<t>吗? Now even if you have a case where that is needed you can just use template <class T1, class T2> .现在,即使您遇到需要的情况,您也可以使用template <class T1, class T2>

Now the fixed implementation would look like this:现在固定的实现看起来像这样:

template <class T>
class SkipList {
    int MAX_LEVEL;
    float p;
    Node<t>* header;
    int level;
public:
    SkipList<T>(int,float) { /* Include definition here */}
    int randomlevel() { /* Include definition here */}
    void insert(T) { /* Include definition here */}
    void remove(T) { /* Include definition here */}
    bool find(T) { /* Include definition here */}
    void print() { /* Include definition here */}
    Node<T>* createnode(T, int) { /* Include definition here */}
};

Similarly Node should be fixed as well (include the definition of the constructor).同样, Node也应该固定(包括构造函数的定义)。

An other thing you mentioned in your post is template specialisation.您在帖子中提到的另一件事是模板专业化。 You can read about it here .你可以在这里阅读它。

You also have a memory leak somewhere between your Node / SkipList classes.您的Node / SkipList类之间的某处也有 memory 泄漏。 Consider moving to smart pointers.考虑转向智能指针。

Finally I strongly encourage that you go through the basics of C++ before you continue . 最后,我强烈建议您 go 在继续之前学习 C++ 的基础知识 You can't run before you can walk.在你能走之前你不能跑。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM