简体   繁体   English

将对象嵌入到链表C ++

[英]Embedding object to a linked list c++

I am trying to add on object to a linked list. 我试图将对象添加到链接列表。 The idea of my program is that to make a student information system. 我的程序的想法是建立一个学生信息系统。 In this student information system when there will be a new entry of student then a new object of the class will be created and that object will be the information for a new node in the linked list. 在这个学生信息系统中,当有一个新的学生条目时,将创建班级的新对象,该对象将是链表中新节点的信息。 In other words the object will be the information field of the nodes of the linked list. 换句话说,对象将是链接列表的节点的信息字段。 I have tried this program but got an error. 我已经尝试过该程序,但出现错误。

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

The error is: 错误是:

1) Not an allowed type 2) Illegal structure operation 1)不允许的类型2)非法结构操作

ret is a member of result that returns void . ret是返回voidresult成员。 I am guessing you meant to make a constructor of result that takes two arguments. 我猜你打算使result的构造函数带有两个参数。 So, this is what you would do 所以,这就是你要做的

class result
{
 int age;
 char name[30];
 float marks;

 public:
 result(){}
 result(int a, float m)
 {
  age = a;
  marks = m;

 }
};

And change t->info = ob.ret(g,ma); 并更改t->info = ob.ret(g,ma); to t->info = result(g, ma); t->info = result(g, ma);

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

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