This code works:
class Student{
public:
int id;
string name;
string city;
Student(int id, string name, string city){
this->id = id;
this->name = name;
this->city = city;
}
};
class Node{
public:
Student* student;
Node* next;
};
int main(){
Student s1(1,"abc","def");
Node n;
n.student = &s1;
n.next = NULL;
}
However if I make the following change:
class Node{
public:
Student student;
Node* next;
};
int main(){
Student s1(1,"abc","def");
Node n;
n.student = s1;
n.next = NULL;
}
I get the error:
<source>: In function 'int main()':
<source>:24:10: error: use of deleted function 'Node::Node()'
24 | Node n;
| ^
<source>:16:7: note: 'Node::Node()' is implicitly deleted because the default definition would be ill-formed:
16 | class Node{
| ^~~~
<source>:16:7: error: no matching function for call to 'Student::Student()'
<source>:9:5: note: candidate: 'Student::Student(int, std::string, std::string)'
9 | Student(int id, string name, string city){
| ^~~~~~~
<source>:9:5: note: candidate expects 3 arguments, 0 provided
<source>:4:7: note: candidate: 'Student::Student(const Student&)'
4 | class Student{
| ^~~~~~~
<source>:4:7: note: candidate expects 1 argument, 0 provided
<source>:4:7: note: candidate: 'Student::Student(Student&&)'
<source>:4:7: note: candidate expects 1 argument, 0 provided
I don't understand why when using a reference instead of a pointer would cause an error related to the constructor of Node.