I am very new to using the STL list library. From what I have been reading here and other places on the web is that I have to create a compare function to use within the sort member function of list. The reason being that the list node's contain structures. I have to sort the list by a few different variables. Below is the class that controls my list. I am getting two errors.
Error 1) error C3867: 'Standings::compareForSort': function call missing argument list; use '&Standings::compareForSort' to create a pointer to member
Error 2) error C2660: 'std::list<_Ty>::sort' : function does not take 1 arguments
Any help here would be appreciated.
#ifndef STANDINGS_H
#define STANDINGS_H
#include <iostream>
#include <list>
#include <fstream>
#include <iomanip>
using namespace std;
#define NAMELENGTH 20
struct Records
{
int totalWin;
int totalLose;
double winPercentage;
double gamesBehind;
int streakWin;
int streakLose;
int streakLength;
int homeWin;
int homeLose;
int awayWin;
int awayLose;
int interWin;
int interLose;
int division;
char teamName[NAMELENGTH];
char streakType;
};
class Standings
{
private:
list<Records> recordsList;
friend class Team;
public:
Standings();
~Standings(){};
void loadList();
void saveList();
void printStandings();
bool compareForSort (Records&, Records&);
};
#endif
#include "standings.h"
Standings::Standings()
{
}
void Standings::loadList(){
//Create a variable of ifstream type names loadFile
ifstream loadFile;
//Open standings txt file
loadFile.open("standings.txt");
//Temporary object of struct type Record
Records r;
//Loop to take from file put into class
//and than put class into list
while(!loadFile.eof()){
loadFile >> r.teamName
>> r.totalWin
>> r.totalLose
>> r.winPercentage
>> r.gamesBehind
>> r.streakWin
>> r.streakLose
>> r.streakType
>> r.streakLength
>> r.homeWin
>> r.homeLose
>> r.awayWin
>> r.awayLose
>> r.interWin
>> r.interLose
>> r.division;
//Checks to make sure its not the end of file.
//If its not the end of the file it inserts
//the node at the end of the list
if(!loadFile.eof())
recordsList.push_back(r);
}
//closing the text file
loadFile.close();
}
void Standings::saveList()
{
//Sorts the List before saving to txt
recordsList.sort(compareForSort);
//Create a variable of ofstream type names saveFile
ofstream saveFile;
//Open standings txt file
saveFile.open("standings.txt");
//Create an iterator to keep place of the nodes saved
list<Records>::iterator i;
//Loop through each node and save each member to that node
for ( i=recordsList.begin(); i != recordsList.end(); ++i ) {
saveFile << right << setw(13) << i->teamName << setw(4) << i->totalWin
<< setw(4) << i->totalLose << setw(7) << setprecision(3) << i->winPercentage
<< setw(6) << i->gamesBehind << setw(2) << i->streakWin
<< setw(2) << i->streakLose << setw(2) << i->streakType
<< setw(2) << i->streakLength << setw(3)<< i->homeWin
<< setw(3) << i->homeLose << setw(3) << i->awayWin
<< setw(3) << i->awayLose << setw(3)<< i->interWin
<< setw(3) << i->interLose << setw(2) << i->division
<< endl;
}
//Flush and Close txt file
saveFile.flush();
saveFile.close();
}
void Standings::printStandings()
{
list<Records>::iterator i;
//Looping through each node and printing each variable of each node
for ( i=recordsList.begin(); i != recordsList.end(); ++i )
{
cout << right << setw(13) << i->teamName << setw(4) << i->totalWin
<< setw(4) << i->totalLose << right << setw(7) << i->winPercentage
<< right << setw(6) << i->gamesBehind << right << setw(2) << i->streakWin
<< right << setw(2) << i->streakLose << right << setw(2) << i->streakType
<< right << setw(2) << i->streakLength <<right << setw(3)<< i->homeWin
<< right << setw(3) << i->homeLose << right << setw(3) << i->awayWin
<< right << setw(3) << i->awayLose << right << setw(3)<< i->interWin
<< right << setw(3) << i->interLose << right << setw(2) << i->division
<< endl;
}
}
bool Standings::compareForSort(Records& node1, Records& node2)
{
list<Records>::iterator i;
for ( i=recordsList.begin(); i != recordsList.end(); ++i ) {
if((node1.totalWin > node2.totalWin) && (node1.winPercentage > node2.winPercentage) && (node1.division < node2.division))
return true;
else
return false;
}
}