c++ linked lists
ok im working on this project with linked lists for my C++ class and the point of the program is to load data from a text document and create a linked list from that data which resembles the train. The linked list is a chain of Node objects (a class i defined which contains a pointer to the next node in the linked list and a car class (which contains a type of data and the amount of data numbers)). Heres my dillemma: my schools CS website appears to be down so i cant access any of the examples or anything, im supposed to have data input from another text document so that the train stops at various "stations" and buys and sells some of its stuff. im starting on the Sell function and so far all im getting is a seg fault error. i have a good idea what to do next, but i want the seg fault to go away before i start doing anymore things. heres my code so far for the sell function:
void Train::SellItems(std::string typeitem, int numitems){ Node *prev, *cur; prev=front; cur=prev->link; for (int i = 0; i < size-1; i++){ while (numitems!=0){ if (cur->car.GetType() == "typeitem"){ //**************** if (typeitem=="grain"){ } //more empty if statements are here... } prev=prev->link; cur=cur->link; } } } the error occurs where i have the commented *******, can anyone tell me why i cant do that? (GetType() is a public member function) thanks |
i accidentally left an extra } or two in there from my if statements, ignore those hehe
|
It's hard to tell based on the few lines posted, but I do see one possible problem.
When you execute this statement Code:
prev=front; Code:
if (cur->car.GetType() == "typeitem"){ //**************** |
hmm ok, i think ill try to get through the list using a for loop, ill just have to rethink my logic a little. the way i had it before had too much annoying stuff.
|
It's been a while since I constructed a list. I just remember how to do it in C, and not C++ because I'm not familar with C++ file I/O, especially fsteams. Maybe this will help in some way.
Code:
#include<stdio.h> Code:
234 name1 |
And ofcourse I forgot to free the list memory
Code:
void FreeList(Node_t *headp) { |
I remember once that I had made a Standard C++ class with a private linked list. The self referential structure was a member of the implementation and the list methods contained the behavior for adding, sorting, and deleting nodes. I have a final exam next wednesday otherwise I'd try to figure out how I made that class. It was cool.
|
With pointers, always initialize to NULL when you create them:
MyClass * foo = NULL; And *always* check pointers to see if they are null before you do any kind of class reference with them. Anytime you do: foo->bar() do: if (foo != NULL) first. It will save you many headaches :) |
Your shouldnt use for loops to loop over linked lists. You need a while loop as gtk uses:
Code:
while (headp) { |
Try and post a better discription of the problem, along with struct/class definitions. It's kinda hard to help when you've only posted a small piece of the program.
Also, try and use some kind of common indentation scheme like gtkuser uses, it's sooooo much easier to read. |
hey thanks for the help, it took a while but i finally figured it out, i ended up taking the while approach. thanks guys
|
All times are GMT -5. The time now is 12:30 AM. |