Я пытаюсь попробовать этот новый проект, что меня представили некоторое время назад, но я не уверен, что здесь происходит. Я уверен, что я могу хранить int в int varible, но он говорит мне, что я не могу преобразовать из int в ученика, и я не уверен, что он пытается сказать мне. Может кто-то здесь, пожалуйста, объясните мне, что именно это пытается сказать мне или что мне не хватает?
#include <iostream>
using namespace std;
class student
{
public:
int id; //student ID number
string name; //students name
string university; //student university
};
//student list is a doubly linked list of students.
class studentList
{
private:
class node
{
public:
student data;
node * next;
node * prev;
};
node * head;
public:
studentList()
{
head = NULL;
}
//be sure to free all dynamically allocated memory!
~studentList();
//return true if the list is empty, false if not
bool empty()
{
if(head == NULL)
return true;
else
return false;
};
//insert student s into the front of the linked list
void push(student s)
{
node * nodeptr;
nodeptr = new node();
nodeptr->data = s;
nodeptr->next = head;
head = nodeptr;
nodeptr->prev = head;
if (nodeptr->next != NULL)
nodeptr->next->prev = nodeptr;
};
//remove and return the student at the front of the list
student pop()
{
node * nodeptr;
int y;
nodeptr = head;
if (head->next != NULL)
head->next->prev = head;
head = head->next;
y = nodeptr->data.id;
delete nodeptr;
return y;
};
//locate and remove the student with given ID number
void removeStudent(int id);
//locate and return a copy of the student with given ID number
student getStudent(int id);
//arrange students into increasing based on either ID or name. If
//variable 'field' has value "id", sort into increasing order by id.
//If 'field' has value "name", sort into alphabetical order by name.
void sort(string field);
//a test function to simply display the list of students to the screen
//in the order they appear in the list.
void display();
};
В вашей функции student pop()
student pop()
{
node * nodeptr;
int y;
nodeptr = head;
if (head->next != NULL)
head->next->prev = head;
head = head->next;
y = nodeptr->data.id;
delete nodeptr;
return y;
};
Вы пытаетесь вернуть int y
где, как вы сказали, тип возвращаемого типа должен быть типом student
поэтому, если вы хотите вернуть int y
тогда вы должны изменить это на
int pop()
{
node * nodeptr;
int y;
nodeptr = head;
if (head->next != NULL)
head->next->prev = head;
head = head->next;
y = nodeptr->data.id;
delete nodeptr;
return y;
};
если вы хотите вернуть ученика, вы можете сделать это
student pop()
{
node * nodeptr;
student y;
nodeptr = head;
if (head->next != NULL)
head->next->prev = head;
head = head->next;
y = nodeptr->data;
delete nodeptr;
return y;
};
Вы объявляете, что ваш метод pop() возвращает ученика, но он возвращает int.
student pop()
{
node * nodeptr;
int y;
nodeptr = head;
if (head->next != NULL)
head->next->prev = head;
head = head->next;
y = nodeptr->data.id;
delete nodeptr;
return y; // not an object of student type!!!
};
Вы должны вернуть nodeptr вместо его удаления.
student y
то вам следует присвоить «y = nodeptr
и вернуть его (конечно, не удаляя «nodeptr»)