Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Please Help! Linked List with Classes Problem

Options
  • 04-12-2002 3:07pm
    #1
    Closed Accounts Posts: 867 ✭✭✭


    I would really appreciate some help with this small problem, that I have encountered.

    I am trying to declare a linked list with a class of members in it. This is my problem:

    I have to initalise the membership class in the linked list, therefore the linked list must come after the class. But I need to declare the linkedlist in the public function prototype of new_membership in the class.

    Anyone see a way in which I can have my linked list with the membership class in it? Please help if you can :)

    Here is the start of the program:

    #include <stdio.h>
    #include <iostream.h>
    #include <stdlib.h>
    #include <string.h>
    #include <iomanip.h>


    class membership
    {
    public:
    membership();
    ~membership();

    void new_membership(LISTNODEPTR *);
    void display();

    private:
    int account_number;
    char first_name[20];
    };

    struct listnode
    {
    membership member;
    struct listnode *nextptr;
    };
    typedef struct listnode LISTNODE;
    typedef LISTNODE *LISTNODEPTR;


    int main()
    {
    int i,j,chc;
    LISTNODEPTR member_list = NULL;
    do
    {
    chc = menu();

    switch (chc)
    {
    case 1:
    void new_membership(&member_list);
    break;
    .
    .
    .


    Am I going about this the wrong way, If you can help me at all, I would really apreciate it

    Brian


Comments

  • Closed Accounts Posts: 5,564 ✭✭✭Typedef


    Behold the shoe.... ah yes... the shoe...

    http://www.boards.ie/vbulletin/showthread.php?s=&threadid=67660

    "
    I'm not an addict... I'm not... I'm not...
    Can I get some more coffee?
    "


  • Closed Accounts Posts: 867 ✭✭✭l3rian


    Thanks for your help typedef

    I couldnt find what I was looking for in that link, but I have abandoned the idea of a linked list containing a class. It doesnt seem possible because one must requires the other to be defined first. So, instead I have initalised an array of classes, not as memory effectant, but simpler to do


  • Registered Users Posts: 4,676 ✭✭✭Gavin


    yeah, don't like using structs meself.
    would stick it into a class,

    and when you are creating a class A that references B, & B references A you have to forward declare the class.

    have a look around on the internet for more details.

    basicly before you declare your class, you do

    class LinkedList ;

    class membership
    {

    } ;

    Gav


  • Registered Users Posts: 95 ✭✭Mr.StRiPe


    This should do it. Only one extra line (in red) enjoy!


    #include <stdio.h>
    #include <iostream.h>
    #include <stdlib.h>
    #include <string.h>
    #include <iomanip.h>


    class membership
    {

    struct LISTNODEPTR;

    public:
    membership()
    ~membership()

    void new_membership(LISTNODEPTR *);
    void display();

    private:
    int account_number;
    char first_name[20];


    };

    struct listnode
    {
    membership member;
    struct listnode *nextptr;
    };


    typedef struct listnode LISTNODE;
    typedef LISTNODE *LISTNODEPTR;


    void main()
    {
    int i,j,chc;

    LISTNODEPTR member_list = NULL;

    while(true)
    {
    chc = menu();

    switch (chc)
    {
    case 1:
    void new_membership(&member_list); break;

    }
    }

    }


  • Registered Users Posts: 95 ✭✭Mr.StRiPe


    This should do it. Only one extra line (in red) enjoy!


    #include <stdio.h>
    #include <iostream.h>
    #include <stdlib.h>
    #include <string.h>
    #include <iomanip.h>


    class membership
    {

    struct LISTNODEPTR;

    public:
    membership()
    ~membership()

    void new_membership(LISTNODEPTR *);
    void display();

    private:
    int account_number;
    char first_name[20];


    };

    struct listnode
    {
    membership member;
    struct listnode *nextptr;
    };


    typedef struct listnode LISTNODE;
    typedef LISTNODE *LISTNODEPTR;


    void main()
    {
    int i,j,chc;

    LISTNODEPTR member_list = NULL;

    while(true)
    {
    chc = menu();

    switch (chc)
    {
    case 1:
    void new_membership(&member_list); break;

    }
    }

    }


  • Advertisement
  • Closed Accounts Posts: 7,563 ✭✭✭leeroybrown


    If you're trying to create a list of Classes you might as well use OO to create the list too.

    A list container and a listItem to store the object


  • Closed Accounts Posts: 9,314 ✭✭✭Talliesin


    template<class T> class listNode
    {
    T member;
    listNode<T>* nextNode;
    public:
    /*various member functions*/
    }

    Then for any class (say myObj*) you can create a listnode (of class listNode<myObj*>).

    This is how the STL list works, which is what you generally use for real-world code anyway, rather than creating YET ANOTHER list class for the next person to deal with the code to work out (or worse, make false assumptions about based on how he or she codes lists).


  • Closed Accounts Posts: 7,563 ✭✭✭leeroybrown


    Indeed, very true.

    My brain was in "homework mode" ...

    Programming Labs in my experience tend to require everything in first principles.

    (Reinventing the wheel)


  • Closed Accounts Posts: 9,314 ✭✭✭Talliesin


    Of course. You shouldn't reinvent the wheel, but you should be able to reinvent the wheel!

    Anyway, your wheel should use templates ;)


  • Closed Accounts Posts: 7,563 ✭✭✭leeroybrown


    We're agreed

    It wasn't the best piece of advice I've ever given ... ;)


  • Advertisement
Advertisement