2013-10-14 28 views
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(); 
}; 
+0

发布到StackOverflow时,如果发生编译错误,应该包含实际的编译器错误,如果可能的话,还应包含[sscce](http://sscce.org/)。 – kfsone

回答

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 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; 
}; 
0

你声明你的pop()方法正在返回student,但它返回的是一个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而不是删除它。

+0

我注意到,我试图编辑y部分来说学生y,它拿了它,但我到底在做什么?所以我想 –

+0

如果你说'学生y'那么你应该分配'y = nodeptr'并返回它(当然没有删除nodeptr) – Ashalynd

+0

那么我删除它的原因是因为我需要根据它去掉它到任务。对我来说没什么大不了的,但他们告诉我如果我不这样做会造成内存泄漏。不知道这是否是完全正确的。 –