So I have a class named Student and I have to make a list of students using linked lists.
Below you have my code:
#include <string>
#include <iostream>
using std::string;
using std::cout;
using std::endl;
class Student
{
private:
int grade;
string name;
public:
Student(int grade, string name)
{
this->grade = grade;
this->name = name;
}
void getStudent()
{
cout << name << grade << endl;
}
};
class Node {
public:
Student student;
Node* next;
};
void printList(Node* n)
{
while (n != NULL)
{
n->student.getStudent();
n = n->next;
}
}
int main()
{
Node* studentList = NULL;
studentList = new Node(); // Reported error at this line
studentList->student = Student(1, "Matei");
printList(studentList);
}
However I get an error: the default constructor of "Node" cannot be referenced -- it is a deleted function.
Please help me!
std::list<Student>is a linked list ofStudents, though you'd probably run into the same error. When you create aNodeyou need to create itsStudentmember. How do you want to create it? (hint: you need to call one of its constructors)Nodedirectly contains aStudent, and thisStudentmust be constructed before the body of theNodeconstructor is entered. SinceStudentdoes not have a default constructor, it cannot be automatically constructed, so the default constructor ofNodecannot be used. You will have to specify the arguments for theStudentconstructor in theNodeconstructor's Member Initializer List.