1

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!

3
  • std::list<Student> is a linked list of Students, though you'd probably run into the same error. When you create a Node you need to create its Student member. How do you want to create it? (hint: you need to call one of its constructors) Commented Nov 8, 2020 at 23:16
  • 1
    Node directly contains a Student, and this Student must be constructed before the body of the Node constructor is entered. Since Student does not have a default constructor, it cannot be automatically constructed, so the default constructor of Node cannot be used. You will have to specify the arguments for the Student constructor in the Node constructor's Member Initializer List. Commented Nov 8, 2020 at 23:18
  • einpoklum has a better idea in their answer and is aiming for Aggregate Initialization Commented Nov 8, 2020 at 23:34

1 Answer 1

3

When we compile your code (GodBolt.org), we get:

<source>:44:28: error: use of deleted function 'Node::Node()'
   44 |     studentList = new Node(); // Reported error at this line
      |                            ^
<source>:25:7: note: 'Node::Node()' is implicitly deleted because the default definition would be ill-formed:
   25 | class Node {
      |       ^~~~
<source>:25:7: error: no matching function for call to 'Student::Student()'
<source>:13:5: note: candidate: 'Student::Student(int, std::string)'
   13 |     Student(int grade, string name)

Let me interpret that for you:

You were trying to construct an instance of Node using the default (no-argument) constructor: Node(). But - does Node have a default constructor? You would think that it should, because you haven't deleted it, nor defined any other constructor.

... but this would be a mistake. You see, one of the fields of node is a Student; and the implicit default constructor of Node constructs the fields of Node using their own default constructors. Unfortunately, you have implicitly deleted the default constructor of Student by defining a constructor of your own.

So, what should you do?

  1. Make the Node constructor take a Student (or const Student& etc.).

  2. Change the last lines in your code to something like

    studentList = new Node(Student(1, "Matei"));
    printList(studentList);
    

PS - In examples here on the site, please use namespace-qualified identifiers such as std::cout and std::string, and include their relevant standard-library headers. Don't just write cout or string.

Sign up to request clarification or add additional context in comments.

4 Comments

I think you mean to have curly braces rather than regular brackets. new Node{...}. Might as well go the distance with new Node{{1, "Matei"}, nullptr};
Thank you for the answer, @user4581301 yes it has to be curly braces.
Can you guys help me with something else?
@MateiAnghel: Ask another question if you have another problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.