私は「think like a programmer」という本からいくつかのエクササイズをしています。 私はクラスの章を始めました。ここでは、私はコードをコンパイルしている間に私の頭を包み込むことができないので固執しているようです。学習C++:エラー:削除された関数の使用
ここにコードがあります。それは私のものではない、私はそれを理解しようとしている本に書いている。
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()’
をデータメンバ
studentRecord studentData
の初期化子を提供し、集計の初期化を使用してエラーを回避することができます生の所有者のポインタを使用して、メモリをリークして、それを投げ捨て、[より良い本を得る](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list)。 – nwpなぜ、他の多くの人がここに投稿しているのですが、実際のコードを投稿するのが難しく、プリプロセッサディレクティブを完成させるのはなぜですか? –
'NULL'の代わりに' nullptr'を使用してください。 –