Linked list of a class object?....

This is a discussion on Linked list of a class object?.... within the C++ Programming forums, part of the General Programming Boards category; I have a ordered linked list templated out and working great. As of now I have a really simple node ...

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    131

    Linked list of a class object?....

    I have a ordered linked list templated out and working great. As of now I have a really simple node defined as such
    Code:
    template <class Type>
    struct nodeType
    {	
    	Type info;
    	nodeType<Type> *link;
    };
    What I am now trying to accomplish is making this linked list a list of another class I am using to get and set data. So each node would be a class. I think This other class is called Point. In this class I have a few functions that get and set the data. I want to be able to access the set function of the point class in each indvidual node. I hope this makes sense. Anyways I am a little confused on how to accomplish this. Do I create a pointer to my Point class in the node?

    Thanks

    Chad

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    'info' will be a Point. So you can call Point functions with info, e.g.:

    current->info.setCoordinates()

    where current is a pointer to a node in your list.
    Last edited by 7stud; 12-08-2005 at 12:14 PM.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    131
    Quote Originally Posted by 7stud
    'info' will be a Point. So you can call Point functions with info, e.g.:

    current->info.setCoordinates()

    where current is a pointer to a node in your list.
    It makes sense, honest, but I just don't know how to get there.
    Code:
    #include Point.h
    
    template <class Type>
    struct nodeType
    {	
    	Point info;
    	nodeType<Type> *link;
    };
    ?

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Start over with the code in your first post. How do you employ your template to declare a node for an int?
    Last edited by 7stud; 12-08-2005 at 02:32 PM.

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    131
    Well I have two classes that deal with my linked list. One is called linkedList and the other is inherited from that called orderedLinkedList. When I want to declare a node as an int I do this
    Code:
    orderedLinkedListType<int> list1
    Hope that answers your question.

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    When I want to declare a node as an int I do this
    Code:
    orderedLinkedListType<int> list1
    That declares an "orderedLinkedListType". What do you need to do to declare a nodeType that contains an int?

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    ...also get rid of "Type" in your class and struct names. It's unnecessary and distracting. Anyone who reads your code should know that a class or struct defines a type, and if they don't, putting Type in the name isn't going to help them. In addition, class and struct names are usually capitalized.
    Last edited by 7stud; 12-08-2005 at 03:23 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List Not Saving Value as Int
    By bar338 in forum C Programming
    Replies: 4
    Last Post: 05-04-2009, 08:53 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  3. circular doubly linked list help
    By gunnerz in forum C++ Programming
    Replies: 5
    Last Post: 04-28-2007, 09:38 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 02:27 PM
  5. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 09:07 PM

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