使用带有自定义模板容器的自定义类

Using a custom class with a custom template container

本文关键字:自定义      更新时间:2023-10-16

我试图定义一个自定义模板容器,然后添加自定义类对象(学生)到它。下面是我的代码:

class Student{
public:
    string subject;
    Student(string _subject){
        subject = _subject;
    }
};

这是我的LinkedList模板代码

template <class T>
class LinkedList{
private:
    struct Node{
        Node *next;
        T value;
    };
    Node *root;
    Node *curr;
    int count;
public:
    LinkedList() : count(0), root(NULL){}
    void add(T val){
        if (root == NULL){
            root = new Node;
            root->value = val;
            root->next = NULL;
            curr = root;
        }
        else{
            curr->next = new Node;
            curr = curr->next;
            curr->value = val;
            curr->next = NULL;
        }
        count++;
    }
    void print(){
        for (Node *itr=root; itr != NULL; itr = itr->next){
            cout << itr->value << endl;
        }
    }
};
int main(){
    LinkedList<Student> a;
    Student sam("Math");
    a.add(sam)
}

当我运行它时,我得到

LinkedList .cpp:在构造函数' LinkedList::Node::Node() '中:LinkedList .cpp:27:实例化从' void LinkedList::add(T) [with T = Student] '

linkedlist.cpp:133: instantiated from here

linkedlist.cpp:27:错误:没有匹配的函数调用' Student::Student() 'linkedlist.cpp:18:注:候选人为:Student::Student(std::string)

linkedlist.cpp:15:注:Student::Student(const Student&)LinkedList .cpp:在成员函数' void LinkedList::add(T) [with T = Student] '中:

LinkedList .cpp:40:注意:合成方法' LinkedList::Node::Node() '首先需要在这里

我不知道这个错误到底是什么意思,也不知道它要求我做什么。如果我这样做:

int main(){
    LinkedList<Student*> a;
    Student sam("Math");
    a.add(&sam);
}

它工作得很好。

但是存储对对象的引用不是我所追求的。我怎样才能让我的LinkedList复制我想添加的对象?

提前感谢!

首先…

#include <utility>

现在,改变这个…

struct Node {
    Node* next;
    T     value;
};

……

struct Node {
    Node* next;
    T     value;
    inline Node(const T &value) : value(value), next(nullptr) {}
    inline Node(T &&value) : value(std::move(value)), next(nullptr) {}
};

,这…

void add(T val) {
    if(this->root == nullptr) {
        this->root = new Node;
        this->root->value = val;
        root->next = NULL;
        curr = root;
    } else {
        curr->next = new Node;
        curr = curr->next;
        curr->value = val;
        curr->next = NULL;
    }
    count++;
}

这个……

void add(const T &val){
    if(this->root == nullptr) {
        this->root = new Node(val);
        this->curr = root;
    } else {
        this->curr->next = new Node(val);
        this->curr = this->curr->next;
    }
    this->count++;
}
void add(T &&val){
    if(this->root == nullptr) {
        this->root = new Node(std::move(val));
        this->curr = root;
    } else {
        this->curr->next = new Node(std::move(val));
        this->curr = this->curr->next;
    }
    this->count++;
}

所发生的是你用new Node和它的隐式默认构造函数创建了一个Node对象。这意味着Node::value的默认构造函数被调用。那么,你能告诉我有没有吗?不。Student没有默认构造函数,所以这不起作用。

顺便说一句,我对你的代码做了一些小的重新设计,所以为了避免出现的几个问题。作为奖励,您现在可以使用移动语义(试试list.add(Student("Joe")))!

还要记住在构造函数初始化列表中将curr初始化为nullptr !

Student必须提供一个默认构造函数。试:

Student(string _subject = "")

因为当您执行new Node时,它创建了一个Node对象,因此它创建了Node类的Node*T属性。如果T类没有默认构造函数,则编译器不知道如何创建该对象。

错误信息是这样写的:

error: no matching function for call to ‘Student::Student()’ linkedlist.cpp:18: note: candidates are: Student::Student(std::string)

它找不到默认构造函数Student::Student(),它只找到了一个以字符串作为参数的Student::Student(std::string)