简体   繁体   中英

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

ret is a member of result that returns void . I am guessing you meant to make a constructor of result that takes two arguments. 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); to t->info = result(g, ma);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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