shynthriir
July 2nd, 2009, 02:42 PM
Been playing with linked lists here and trying to clean up a small bit of code I have, the best way would be to break it up into a couple different functions, but this requires me to pass a linked list to a function. Here's some code bits and how I *think* I'm supposed to do it, but g++ gives me a compile error when I try to compile it.
This struct is in main().
struct movie {
string name; // movie name
float runtime; // movie runtime (length) in minutes
int rating; // user's person rating of movie (1/2/3/4/5)
struct movie *next; // pointer to the next struct in list
};
For this example I want to have a function that prints the data from the list to the screen.
Function Protocall:
void printList(struct movie* firstInList, int listLength);
Function block:
void printList(struct movie* firstInList, int listLength);
// loop priming
struct movie* currentListItem = firstInList;
cout << currentListItem -> name << endl;
cout << currentListItem -> runtime << " Minutes" << endl;
cout << currentListItem -> rating << " Stars" << endl << endl;
// end of loop priming
for (int i = 1; i < listLength; i++) {
currentListItem = currentListItem -> next;
cout << currentListItem -> name << endl;
cout << currentListItem -> runtime << " Minutes" << endl;
cout << currentListItem -> rating << " Stars" << endl << endl;
}
}
It complains about the void type on the function, which I know I can change to int (not sure why it doesn't like void type-returning functions honestly). But even if I change that to, say, int, the problem that I don't quite get relates to the
currentListItem -> name
bits. Part of me says it has something to do with scope, but I really don't know. Any help?
This struct is in main().
struct movie {
string name; // movie name
float runtime; // movie runtime (length) in minutes
int rating; // user's person rating of movie (1/2/3/4/5)
struct movie *next; // pointer to the next struct in list
};
For this example I want to have a function that prints the data from the list to the screen.
Function Protocall:
void printList(struct movie* firstInList, int listLength);
Function block:
void printList(struct movie* firstInList, int listLength);
// loop priming
struct movie* currentListItem = firstInList;
cout << currentListItem -> name << endl;
cout << currentListItem -> runtime << " Minutes" << endl;
cout << currentListItem -> rating << " Stars" << endl << endl;
// end of loop priming
for (int i = 1; i < listLength; i++) {
currentListItem = currentListItem -> next;
cout << currentListItem -> name << endl;
cout << currentListItem -> runtime << " Minutes" << endl;
cout << currentListItem -> rating << " Stars" << endl << endl;
}
}
It complains about the void type on the function, which I know I can change to int (not sure why it doesn't like void type-returning functions honestly). But even if I change that to, say, int, the problem that I don't quite get relates to the
currentListItem -> name
bits. Part of me says it has something to do with scope, but I really don't know. Any help?