我正在从“像程序员一样思考”一书中做一些练习,到目前为止一切都很棒。 我开始上课章节,在这里我似乎陷入困境,因为我无法解决我正在编译代码的错误。
这是代码。它不是我的,我一直在书中试图理解它。
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()’
答案 0 :(得分:4)
定义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);
};
答案 1 :(得分:2)
studentRecord
不是默认构造的,因为您提供了一个用户构造函数(studentRecord(int a, int b, string c);
)。因此studentNode
不能有编译器生成的默认构造函数。自己提供一个,或者给studentRecord
一个默认的构造函数。
答案 2 :(得分:1)
结构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;
}