I'm at an absolute complete loss with this. I'm a using a pointer-pointer to "remember" the last node of a linked list so a function in the loop can add another node and then return the address of that new node so it's known for the next loop.
Sounds easy, right? Well i've found a way to mess it up :(.
The real program is spread across multiple files and i'd hate to go overkill with 500+ lines so i've cut it all down to what I hope are the bare essentials required for this problem to be fixed. It's hard sifting through lots of code and working out what will and won't be required to form the mental image required.
Out of all the mess below the part I want to draw your attention to is the for() loop in the Load() function right at the bottom.
class ScoreEntry
{
protected:
ScoreEntry *Nextnode;
public:
int Score;
string Name;
int Kills;
ScoreEntry();
ScoreEntry** Load(ifstream& infile, ScoreEntry **currEntry);
ScoreEntry* GetNext();
void SetNext(ScoreEntry *next);
};
ScoreEntry::ScoreEntry()
{
Name = "";
Score = 0;
Kills = 0;
Nextnode = 0;
}
ScoreEntry** ScoreEntry::Load(ifstream& infile, ScoreEntry **currEntry)
{
// new node to store the data in
ScoreEntry* newEntry = new ScoreEntry;
// read in data to the new node
infile >> newEntry->Name;
infile >> newEntry->Kills;
infile >> newEntry->Score;
// end of file doesn't constitute a read error
if(!infile.good() && !infile.eof()){
delete newEntry;
return 0;
}
if(*currEntry == 0){
// the head is the first node
*currEntry = newEntry;
}else{
// attach the node to end of the list
(*currEntry)->SetNext(newEntry);
}
ScoreEntry** retEntry = &newEntry;
return retEntry;
}
ScoreEntry* ScoreEntry::GetNext()
{
return Nextnode;
}
void ScoreEntry::SetNext(ScoreEntry *next)
{
Nextnode = next;
}
class HighScoreTable
{
public:
HighScoreTable();
int Load();
ScoreEntry *ListHead;
};
HighScoreTable::HighScoreTable()
{
ListHead = 0;
}
int HighScoreTable::Load()
{
int entries = 0;
ScoreEntry **currEntry = &ListHead;
for(int i=0; highScrFile.good() && i<MAXENTRIES; i++){
currEntry = (*currEntry)->Load(highScrFile, currEntry);
if(*currEntry != 0){
entries++;
}
}
return entries;
}
The pointer-pointer currEntry is declared outside of the for() loop, and so should be in-scope until the function returns. The pointer being pointed to however (for some unknown reason) resets after the 1st loop!
ScoreEntry **currEntry = &ListHead;
for(int i=0; highScrFile.good() && i<MAXENTRIES; i++){ // <<<< *currEntry resets after stepping past here! :(
currEntry = (*currEntry)->Load(highScrFile, currEntry);
if(*currEntry != 0){
entries++;
}
}
After
currEntry = (*currEntry)->Load(highScrFile, currEntry);
Has executed *currEntry will show correctly as:
0x055c5fa8 {Score=data Name=data Kills=data ...} ScoreEntry *
but as soon I step past the for() line (as the 2nd iteration begins) *currEntry instantly transforms into:
*currEntry 0xcccccccc {Score=??? Name={...} Kills=??? ...} ScoreEntry *
What the ... ? It's acting as though it's suddenly come out of scope and had to be re-initialised. This completely defeats the purpose of the pointer-pointer as it's supposed to remember the last node of the linked list for the ScoreEntry Load() function.
Whhhyyyyyyyyyyyy !? :(