将对象嵌入到链表中
Embedding object to a linked list c++
我正在尝试添加一个对象到链表。我的程序的想法是做一个学生信息系统。在这个学生信息系统中,当有一个新的学生条目时,将创建一个类的新对象,该对象将是链表中新节点的信息。换句话说,对象将是链表节点的信息字段。我已经尝试了这个程序,但是得到一个错误。
#include<iostream.h>
class result
{
int age;
char name[30];
float marks;
public:
void ret(int a, float m)
{
age = a;
marks = m;
}
};
struct node
{
result info;
struct node *next;
};
void main()
{
struct node *h, *t;
int g;
float ma;
cout<<"Enter age , name , marksn";
cin>>g;
cin>>ma;
result ob;
h = NULL;
t = new node;
t->info = ob.ret(g,ma);
t->next = NULL;
h = t;
cout<<t->info;
}
错误是:
1)不允许的类型2)违规结构操作
ret
是返回void
的result
的成员。我猜你的意思是让result
的构造函数有两个参数。这就是你要做的
class result
{
int age;
char name[30];
float marks;
public:
result(){}
result(int a, float m)
{
age = a;
marks = m;
}
};
并将t->info = ob.ret(g,ma);
更改为t->info = result(g, ma);
相关文章:
- 为什么不能修改对象中的值?另外,我如何改进此链表?
- C++ 创建包含链表和字符串的对象的链接列表时出错
- 链表包含 c++ 中不同的派生类对象
- 如何深度复制链表对象指针
- 如何通过不同的指针使用类的对象访问结构?(链表)(C++)
- 节点链表中的对象
- 按引用或值传递链表对象更好吗?
- 基于范围的 for 循环,用于包含C++中的指针的自定义链表,仅返回对象
- C++ 如何创建链表的副本作为类对象
- 无法分配给链表中的对象
- C++ 链表:"->"运算符无法更改节点对象的内部值
- 具有自己的链表的对象
- 存储类对象的C++模板链表-未处理的异常访问冲突读取位置
- 从链表访问对象(自定义实现)
- 创建面向对象的链表时出现编译问题(编译器错误C2664)
- 对象数组作为链表中的字符数组 - 我对对齐的假设是否有效
- C++ 链表:对象的索引
- 如何将链表对象写入文件
- 在c++中创建一个二维链表对象
- 调用在另一个对象内部定义的链表对象的函数