使用类和类型
Working with Classes and types
本文关键字:类型 更新时间:2023-10-16
我正在尝试这个新项目,这是我不久前被介绍的,但我不确定这里发生了什么。我确信我可以将 int 存储在 int 变量中,但它告诉我我无法从 int 转换为学生,我不确定它想告诉我什么。这里有人可以向我解释一下这到底想告诉我什么或我错过了什么吗?
#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();
};
在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而不是删除它。
相关文章:
- ArduinoJson 6.15.2:JsonObject没有命名类型
- 防止主数据类型C++的隐式转换
- 大量序列中核苷酸类型的快速计数
- 如何从C++中的依赖类型中获得它所依赖的类型
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- 是否可以初始化不可复制类型的成员变量(或基类)
- 如何获取std::result_of函数的返回类型
- 从父命名空间重载类型
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 我想将一个对T类型的非常量左值引用绑定到一个T类型的临时值
- Openssl 1.1.1d无效使用不完整的类型"struct dsa_st"
- 访问者访问变体并返回不同类型时出错
- 在VS2010-VS2015下编译时,如何使用decltype作为较大类型表达式的LHS
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- C++ 雷神库 - 使用资源加载器类时出现问题(不命名类型)
- 模板元程序查找相似的连续类型名称
- 是否可以从int转换为enum类类型
- 构造函数正在调用一个使用当前类类型的函数
- 我应该使用什么来代替void作为变体中的替代类型之一
- 类中的字符串不命名类型