使用带有自定义模板容器的自定义类
Using a custom class with a custom template container
我试图定义一个自定义模板容器,然后添加自定义类对象(学生)到它。下面是我的代码:
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)
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- 如何将点击的信号和插槽添加到qt中的自定义按钮中
- C++自定义比较函数
- 如何比较自定义类的std::变体
- std::设置自定义比较器
- 如何正确实现和访问运算符的各种自定义枚举器
- flutter:即使shouldRepaint()返回true,自定义画家也不会重新绘制
- 自定义先决条件对移动分配运算符有效吗
- 使用VS Code和CMake Tools运行自定义命令
- 如何创建从Maya(或类似程序)到虚幻引擎的自定义数据导出插件
- std::ranges::elements_view,用于自定义类似元组的数据
- 跟随整数索引列表的自定义类迭代器
- 参数化自定义CMake工具链
- 使用自定义比较函数使用std::sort()对矢量字符串进行排序时出现问题
- 如何在自定义类中启用'auto loops'?
- 使用QJsEngine在Qt中注册自定义类型
- Qt自定义QPush按钮未显示在布局上
- 自定义对象的dlib序列化在gcc中失败
- 自定义创建QFuture
- 如何在QT中的自定义视频小工具t上绘制矩形