Error when returning pointer from method, but not when accessing the pointer directly. C++

  c++

When I do this

int main() {

Student *head = new Student();
Student *second = new Student();

head->S_getStudentPtr() = second; //Expression must be a modifiable Ivalue
head->S_nextStudent = second;

delete head;
delete second;
std::cout << std::endl;    
return 0;
}

I get an error when trying to return pointer from method, the error message is shown in the comment.
However, when I am accessing the same pointer directly, it doesn’t complain.

Here is the method:

Student * Student::S_getStudentPtr() {
S_nextStudent = new Student();
return S_nextStudent;
}

I suspect that it is something with the method. But I can’t seem to figure it out.

Thank you in advance.

Source: Windows Questions C++

LEAVE A COMMENT