繁体   English   中英

将对象嵌入到链表C ++

[英]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 , marks\n";
    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是返回voidresult成员。 我猜你打算使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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM