Linked Lists

Hey everyone!
I'm having trouble with linked lists. The compiler error I'm getting is that it's a LINKING problem; I think that something is wrong because I'm accessing too many things (I'm dereferencing temporary to access the class, then I access the struct inside the class to access the name) but I really don't know another way to do it. All help is greatly appreciated.

Basically, I have:


struct info
{
char* name;
};

class person
{
public:
person();
void display();
info data;

private:
int index;
};

struct node
{
person pal;
node* link;
};

void person::display()
{
node* temporary;
temporary = new node;

strcpy (temporary->pal.data.name, "bya");

}




Last edited on
You're not defining person::person().

You have a bunch of uninitialized pointers. Be careful with that. A linked list is a particularly nasty place to have those.
Last edited on
Ah thanks so much for the response. Don't know why I didn't define the constructor.
When you say I have a bunch of uninitialized pointers, which do you mean? For example: name I should use name = new char[some_length]; BUT are you saying I need to initialize temporary as well? Isn't it initialized when I created memory with "temporary = new node;" ? I'm very new to pointers so I'm still trying to get a handle on these kinds of things.
Actually, I overestimated the quantity. You have two uninitialized pointers: info::name, and node::link.
The call to strcpy() will cause a segmentation fault. In the case of links in linked lists you should always initialize them to something. Either to zero, NULL, or a valid pointer, but never leave them uninitialized or you won't be able to know where the list ends. The same goes for similar structures, such as trees and graphs (technically, trees and lists are specific instances of graphs).
Topic archived. No new replies allowed.