0

我正在尝试这个新项目,这是我不久前被介绍的,但我不确定这里发生了什么。我确信我可以将一个 int 存储在一个 int 变量中,但它告诉我我无法从 int 转换为 student,而且我不确定它试图告诉我什么。有人可以向我解释一下这到底是想告诉我什么或我想念什么吗?

#include <iostream>
using namespace std;

class student
{
public:
    int id;            //student ID number
    string name;       //student’s 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();
};
4

2 回答 2

0

您声明您的 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 而不是删除它。

于 2013-10-14T01:26:44.237 回答
0

在你的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 ywhere 正如您所说的返回类型应该是 typestudent所以如果您想返回,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;
};
于 2013-10-14T01:30:57.853 回答