学习C :错误:使用已删除功能
learning c++: error: use of deleted function
我正在做一些练习"像程序员一样思考",到目前为止,一切都很好。我开始了课堂章节,在这里我似乎被卡住了,因为我无法围绕我被编译代码的错误。
这是代码。这不是我的,我一直在书中写它来理解它。
struct studentRecord {
int studentId;
int grade;
string name;
studentRecord(int a, int b, string c);
};
class studentCollection {
private:
struct studentNode {
studentRecord studentData;
studentNode *next;
};
public:
studentCollection();
void addRecord(studentRecord newStudent);
studentRecord recordWithNumber(int idNum);
void removeRecord(int idNum);
private:
//typedef studentNode *studentList;
studentNode *_listHead;
};
studentRecord::studentRecord(int a, int b, string c) {
studentId = a;
grade = b;
name = c;
}
studentCollection::studentCollection() {
_listHead = NULL;
}
void studentCollection::addRecord(studentRecord newStudent) {
studentNode *newNode = new studentNode;
newNode->studentData = newStudent;
newNode->next = _listHead;
_listHead = newNode;
}
studentRecord studentCollection::recordWithNumber(int idNum) {
studentNode *loopPtr = _listHead;
while (loopPtr != NULL && loopPtr->studentData.studentId != idNum) {
loopPtr = loopPtr->next;
}
if (loopPtr == NULL) {
studentRecord dummyRecord(-1, -1, "");
return dummyRecord;
} else {
return loopPtr->studentData;
}
}
int main() {
studentCollection s;
studentRecord stu3(84, 1152, "Sue");
studentRecord stu2(75, 4875, "Ed");
studentRecord stu1(98, 2938, "Todd");
s.addRecord(stu3);
s.addRecord(stu2);
s.addRecord(stu1);
}
我遇到的错误是:
studentclass1.cpp: In member function ‘void studentCollection::addRecord(studentRecord)’:
studentclass1.cpp:45:32: error: use of deleted function ‘studentCollection::studentNode::studentNode()’
studentNode *newNode = new studentNode;
^~~~~~~~~~~
studentclass1.cpp:17:12: note: ‘studentCollection::studentNode::studentNode()’ is implicitly deleted because the default definition would be ill-formed:
struct studentNode {
^~~~~~~~~~~
studentclass1.cpp:17:12: error: no matching function for call to ‘studentRecord::studentRecord()’
定义 struct
时,例如:
struct studentNode {
studentRecord studentData;
studentNode *next;
};
它具有隐式定义的默认构造函数,等效于:
struct studentNode {
studentNode() : studentData(), next() {}
studentRecord studentData;
studentNode *next;
};
这是一个问题,因为studentRecord
的默认构造函数由于存在用户定义的构造函数而被编译器删除。
您可以将默认构造函数添加到studentRecord
以解决该问题。
struct studentRecord {
int studentId;
int grade;
string name;
studentRecord() = default;
studentRecord(int a, int b, string c);
};
而不是使用= default;
指示符使用计算机生成的默认构造函数,而是使用有效数据初始化对象。
struct studentRecord {
int studentId;
int grade;
string name;
studentRecord() : studentRecord(0, 0, "") {} // Delegate to the other constructor.
studentRecord(int a, int b, string c);
};
studentRecord
不是默认构造的,因为您提供了用户构造函数(studentRecord(int a, int b, string c);
(。因此,studentNode
不能具有编译器生成的默认构造函数。自己提供一个或给studentRecord
默认构造函数。
结构studentRecord
具有用户定义构造函数
struct studentRecord {
int studentId;
int grade;
string name;
studentRecord(int a, int b, string c);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
};
在这种情况下,编译器不会生成结构的默认构造函数。
同时在函数addRecord
中void studentCollection::addRecord(studentRecord newStudent) {
studentNode *newNode = new studentNode;
^^^^^^^^^^^^^^^^
newNode->studentData = newStudent;
newNode->next = _listHead;
_listHead = newNode;
}
尝试使用结构studentRecord
的默认构造函数。由于无法使用,编译器将结构studentNode
的默认构造函数定义为已删除。
您可以通过明确提供数据成员studentRecord studentData
的初始化器并使用汇总初始化
可以以以下方式编写功能
void studentCollection::addRecord(studentRecord newStudent) {
studentNode *newNode = new studentNode { newStudent, _listHead };
_listHead = newNode;
}
相关文章:
- 在Qt5中使用QTextSteam时的"使用已删除功能"
- 链表删除功能的单指针 // 是可能的
- 删除功能不适用于串行通信后多个循环中的多个实例
- 为什么每当我尝试运行此链接列表删除功能时都会收到分段错误错误?
- 使用删除功能?
- 二叉搜索树 - 使用 Linux 在虚拟机中制作删除功能
- 为什么我的删除功能总是出现分段错误
- 与 c++ 中的删除功能混淆
- 用户使用C++中的删除功能删除文件
- 所以我想出了他的代码,但我不确定如何继续删除功能?
- 为什么我的删除功能中存在错误
- std::ifstream,使用已删除功能
- 学习C :错误:使用已删除功能
- 升级基于文本游戏的库存中的删除功能
- 错误:使用已删除功能
- 删除功能和ADL
- 队列的删除功能
- 删除功能误差的C 与有条件的iniaization一起使用
- 在PowerPC编译器上删除功能,而不是GCC上的功能
- 为什么是我的AVL树删除功能不平衡