Making LinkList Class Using Pointers.

Started by
16 comments, last by Supaflyfrank 19 years, 7 months ago
Hello I am trying to make a Link List Class with void pointers. I am using void pointers because each Node can be a different class but they all should link together. Also I want the link list class to know what the head is so I decided to have a static void pointer called Head. I have it static because every node has the same head so it would make sense for the head to be a static void pointer. My problem is that I get this linking error when I compile my program. Link List error LNK2020: unresolved token (0A000010) ?Head@LinkList@@2PAXA Link List fatal error LNK1120: 1 unresolved externals Here is the code any help would be most welcome.

#include <iostream>
#include <new>
#include <stdio.h>
using namespace std;
class LinkList
{

  public:
  template <class X> X *CreateObject(X Head);
  template <class X> X *FindEndObject(X *Node);
  template <class X> void AddToLinkList(X *Adding,void *LastObject);
  template <class X> void MakeNewObject(X NewThing);
  void *Next;
  static void *Head;

};
class Player
{
 public:
 LinkList Node;
 char Face[20];
};

int main(int argc, char *argv[])
{
   Player Frank;
   Frank.Node.Head = &Frank

}

template <class X> X *LinkList::CreateObject(X Head)
{
  X *Sample = new X;
  return Sample;
  
}

template <class X> X *LinkList::FindEndObject(X *Node)
{
    X* tmpRoom = static_cast<X*>(Node);
    while(tmpRoom->Next)
    {
      tmpRoom = static_cast<X*>(tmpRoom->Next);
    }
    return tmpRoom;
}

template <class X> void LinkList::AddToLinkList(X *Adding,void *LastObject)
{
  X* Temp = reinterpret_cast<X*>(LastObject);
  Temp->Next = Adding;
}

template <class X> void LinkList::MakeNewObject(X NewThing)
{
  AddToLinkList(CreateObject(NewThing),FindEndObject(Next));

}
Fruny - added code tag. [Edited by - Fruny on September 12, 2004 2:18:56 PM]
Advertisement
Are you sure what you want isn't something like inheritance ? Unless you REALLY want to put ANYTHING in there, it would be muchh, much cleaner.
I teleported home one night; With Ron and Sid and Meg; Ron stole Meggie's heart away; And I got Sydney's leg. <> I'm blogging, emo style
The thing is that the LinkList class will also be used for another group of classes. Like class Food,Eq,Money will be in one link list and another class Room,Store etc.. will be in another link list. And I want the LinkList class to be used to create many link list. That is why I have them as Templates and using void pointers. Because I never know what class the node will be and that their will be different type of classes in one link list so I didnt think they was any other way to solve my problem.
first you've declared a class/static member you have not defined it yet so outside of the class just add:

void* LinkList::Head(0);


Secondly if you really need a list of void pointers you would be better of using the standard library list.

thirdly there are probably a million better ways you could do to avoid such a thing because every time you add an element you've lost its type and you can't guarantee to cast it back to the proper type when needed so you can't for example release the memory you've allocated on the heap properly.
How would I use the standard library to solve this problem? I never seen anything in it that would help me besides using vectors. And I heard some where their is a serious down fall to using vectors something like it takes alot more memory. And these link list will be 1000 plus long so there will already be alot of meomory being used.
How would I use the standard library to solve this problem? I never seen anything in it that would help me besides using vectors.

There is a std::list class template.

And I heard some where their is a serious down fall to using vectors something like it takes alot more memory.

std::vector does over-allocate to allow you to add elements without requiring an allocation every time, but it is not a "serious downfall".

And these link list will be 1000 plus long so there will already be alot of meomory being used.

1000+ elements is not a lot. Unless your objects are tiny, the linked list overhead: two pointers per list object and two pointers per element (it is a doubly-linked list) is not worth losing sleep over.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Quote:Original post by Fruny
How would I use the standard library to solve this problem? I never seen anything in it that would help me besides using vectors.

There is a std::list class template.

And I heard some where their is a serious down fall to using vectors something like it takes alot more memory.

std::vector does over-allocate to allow you to add elements without requiring an allocation every time, but it is not a "serious downfall".

And these link list will be 1000 plus long so there will already be alot of meomory being used.

1000+ elements is not a lot. Unless your objects are tiny, the linked list overhead: two pointers per list object and two pointers per element (it is a doubly-linked list) is not worth losing sleep over.


i'll like to add to that, once you done that and profiled your code and you really do find a performance issue then you can customize STL containers you can give it a custom allocator type, as pointers & smart pointers are small objects & there the same size then you could write or use a pre-written pooled allocator for the container of pointers.
If I used the std::list is there a way the list can contain unknown class types? Or should I just make a list of void pointers and just store the address in the list I guess it would be pretty much the same thing and the fact that I am only storing addresses there shouldnt be any memory problem.
It doesn't make much sense to store "unknown class types". In general, people store pointers to some common base class (not void*!) and rely on polymorphism.

And yes, there can be memory problems: there are situations where a single object can have multiple addresses! Converting between types in the object's hierarchy will fix the address as required, but converting to/from a void* will not.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Are you looking for a generic list that contains items of the same unknown type or a list that can contain items of different unknown types at the same time?
Zeljko

This topic is closed to new replies.

Advertisement