学习C :错误:使用已删除功能

learning c++: error: use of deleted function

本文关键字:删除 功能 错误 学习      更新时间:2023-10-16

我正在做一些练习"像程序员一样思考",到目前为止,一切都很好。我开始了课堂章节,在这里我似乎被卡住了,因为我无法围绕我被编译代码的错误。

这是代码。这不是我的,我一直在书中写它来理解它。

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;
}