Linked Lists Decleratoin from Class

Started by
3 comments, last by Lews_Theirn 20 years ago
I''ve created and tested a doubly linked list. Now I want to call that list from in a class like class something{ private: list *l; public: something(){l = new list();} dosomething(){ /*initialize data*/ l.push(data); } } int main(){ something s; s.printdata(); } However when I try this I''m looping through pushing mutliple nodes in a row my program seg faults in the dosomething function on the section iteration of my loop. Also when I inspect the issue futher the previous pointer has been moved from a valid memmory address to 0x18. Any help on how to declare this list within a class would be helpful. Thank you, Paul
Advertisement
Can you post the header file of your list and your node definition ?
Random questions:
- does the previous pointer of the first node points to 0 (or NULL) ?
- does the next pointer of the last node points to 0 (or NULL) ?
- do you check for 0 or NULL ?
- does the previous pointer of the first node points to the last node, and the next pointer of the last node points to the first node (making it a circular linked list) ? If so, do you have a mean to recognize the first node to avoid cycling infinitely within your list ?

Ghostly yours,
Red.

[EDIT: a few more questions:
- Does happen after eliminating nodes ? If so, do you correctly reassign the pointers ?
- Does it happen when inserting the second node ? If so, do you correctly reassign the pointers ?
Ghostly yours,
Red.
]

[edited by - Red Ghost on April 21, 2004 12:08:22 PM]
Ghostly yours,Red.
As I''ve stated above I''ve tested the list and it works correctly for add, iterate and delete my issue is when declaring the list as a member of another class then running it. And the issue occurs before I eliminate a node in the list. It appears to be a memory referencing error in the class definition because it works if I declare the list in the main file and pass it into the class, such as:

void main(){
list *l = new list();
something *s = new something(l);
dosomething();
printlist();
}

and sorry no I can''t post the header it''s part of a project I have yet to release to the open source community and the head of the project doesn''t want the source out there yet.

PS sorry this isn''t quite a beginner question but I thought someone might have run into the problem before.

thanks,
Paul
I think list is a C++ STL type. You might want to consider capitalizing it (i.e. List).

Assuming it is a problem with your code...
If it works correctly in main, then it may be a problem with the class you are putting it into. Go back to the main implementation, and rigorously test it, then create a whole new class (don't reuse the one that doesn't work), and test it there.

[edited by - Onemind on April 21, 2004 1:59:28 PM]
It works when declared in main and not when declared in Something.
Is your function DoSomething a member function of your Something class ? (does not seem to be in your first post or in your second post).



Ghostly yours,
Red.
Ghostly yours,Red.

This topic is closed to new replies.

Advertisement