【发布时间】:2017-04-17 18:36:00
【问题描述】:
我正在做“像程序员一样思考”一书中的一些练习,到目前为止一切都很好。 我开始了类章节,但在这里我似乎被卡住了,因为我无法解决编译代码时遇到的错误。
这里是代码。这不是我的,我一直在写它试图理解它。
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()’
【问题讨论】:
-
如果这本书真的教你使用原始的拥有指针和内存泄漏,你应该把它扔掉并get a better book。
-
为什么您和其他许多在这里发帖的人发现发布带有预处理器指令的实际代码如此困难?
-
使用
nullptr而不是NULL -
@Gill
studentNode在studentCollection中声明 -
是的。 C++ 程序的功能是完整的;仅取决于它包含的头文件。