Linked list of objects

This is a discussion on Linked list of objects within the C++ Programming forums, part of the General Programming Boards category; Hello I am trying to create a linked list of the following style: Code: #include "variable.h" // CLASS called variable ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    36

    Linked list of objects

    Hello

    I am trying to create a linked list of the following style:
    Code:
    #include "variable.h"  // CLASS called variable
    
    
    struct node{
    	variable *objectPtr;
    	struct node *next;
    };
    typedef struct node nodeptr;
    Basically, a pointer to an object and a pointer to the next node of the list. Is this the correct way of doing it?

    I'm not sure how to set and access the object either. So far I have been trying this but it has not worked

    Code:
    ptr->objectPtr = new variable(VarID,constant);
    Code:
    cout << (ptr->objectPtr).GetVarID() << endl; //GetVarID() is a member function
    Thanks for help

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Since objectPtr is a pointer, why are you not using -> to access it's content (including it's member functions).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    36
    Thanks Mats

    I can confirm that this now works
    Code:
    cout << ptr->objectPtr->GetVarID() << endl;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. single linked list to double linked list (help)
    By Countfog in forum C Programming
    Replies: 8
    Last Post: 04-29-2008, 09:04 PM
  2. Linked List Class with Pointers to Objects and using Polymorphism
    By CaptainMorgan in forum C++ Programming
    Replies: 3
    Last Post: 11-20-2006, 11:41 AM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 11:01 AM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 09:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 11:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21