| | |
Questions on using the STL list sort method
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2009
Posts: 24
Reputation:
Solved Threads: 0
0
#11 28 Days Ago
It appears to be where I call recordsList.sort(&Standings::compareForSort);
> h:\documents\cs132\cs132_project2\cs132_project2\standings.cpp(92) : see reference to function template instantiation 'void std::list<_Ty>::sort<bool(__thiscall Standings:
)(const Records &,const Records &)>(_Pr3)' being compiled
1> with
1> [
1> _Ty=Records,
1> _Pr3=bool (__thiscall Standings:
)(const Records &,const Records &)
1> ]
1>c:\program files (x86)\microsoft visual studio 9.0\vc\include\xutility(348) : error C2064: term does not evaluate to a function taking 2 arguments
> h:\documents\cs132\cs132_project2\cs132_project2\standings.cpp(92) : see reference to function template instantiation 'void std::list<_Ty>::sort<bool(__thiscall Standings:
)(const Records &,const Records &)>(_Pr3)' being compiled1> with
1> [
1> _Ty=Records,
1> _Pr3=bool (__thiscall Standings:
)(const Records &,const Records &)1> ]
1>c:\program files (x86)\microsoft visual studio 9.0\vc\include\xutility(348) : error C2064: term does not evaluate to a function taking 2 arguments
0
#12 28 Days Ago
Post your code as of now.
1) What word becomes shorter if you add a letter to it?
[ Solved by : niek_e, Paul Thompson, SgtMe]
2) What does this sequence equal to : (.5u - .5a)(.5u-.5b)(.5u-.5c) ...
[*solved by : murtan]
3) What is the 123456789 prime numer?•
•
Join Date: Nov 2009
Posts: 24
Reputation:
Solved Threads: 0
0
#13 28 Days Ago
I have the overloaded operator and binary predicate commented out atm so i could work on other parts of the program.
C++ Syntax (Toggle Plain Text)
#ifndef STANDINGS_H #define STANDINGS_H #include <iostream> #include <list> #include <fstream> #include <iomanip> using namespace std; #define NAMELENGTH 20 struct Records { int totalWin; //Each teams total wins int totalLose; //Each teams total loses int streakWin; //wins in last 10 games int streakLose; //loses in last 10 games int streakLength; //Number of wins or loses in //a row int homeWin; //number of games won at home int homeLose; //number of games lost at home int awayWin; //number of wins away int awayLose; //number of loses away int interWin; //Number of interleague games won int interLose;//Number of interleague games lost int division; //Which division each team is. double winPercentage; //Percentage of wins //compared to loses double gamesBehind; //Total number of // games played char teamName[NAMELENGTH]; //Team names char streakType; //If teams streak is winning or //losing /*bool operator <(const Records& node) { return ((totalWin > node.totalWin) && (winPercentage > node.winPercentage) && (division < node.division)); }*/ }; class Standings { private: list<Records> recordsList; //List variable friend class Team; //Allows private member access //to the Team class public: //Constructor Standings(); //Destructor ~Standings(){}; //bool operator <(const Records&); //Takes in list from txt file //and inserts it into the list void loadList(); //Takes in the altered list and //saves to txt file void saveList(); //Prints the list void printStandings(); //Overloaded function to let sort //know how to sort list //Takes in two objects of the struct //Records bool compareForSort (const Records&, const Records&); }; #endif #include "standings.h" //*********************************************************** // Function Name:Standings // Purpose: Constructor //*********************************************************** Standings::Standings() { } /* bool Standings::operator <(const Records& node) { list<Records>::iterator i; //for ( i=recordsList.begin(); i != recordsList.end(); ++i ) { if ((i->totalWin > node.totalWin) && (i->winPercentage > node.winPercentage) && (i->division < node.division)) return true; else return false; //} }*/ //*********************************************************** // Function Name: loadList // Purpose: To pull in info from a txt file and insert it into // a list. // Parameters: None // Return Value: None // Data Members Accessed: struct Records variables // Data Members Modified: recordsList // Functions Called: ifstream.open(), list.push_back(), // ifstream.close() //*********************************************************** void Standings::loadList(){ //Create a variable of ifstream type names loadFile ifstream loadFile; //Open standings txt file loadFile.open("dummy.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(); } //*********************************************************** // Function Name:saveList // Purpose: To put the updated list back into the original // text file // Parameters:None // Return Value: None // Data Members Accessed: struct Records member variables // Functions Called:list.sort(), ofstream.open(), // ofstream.close(), ofstream.flush(), // setw(), list.begin(), list.end() //*********************************************************** void Standings::saveList() { //Sorts the List before saving to txt //recordsList.sort(); recordsList.sort(&Standings::compareForSort); //Create a variable of ofstream type names saveFile ofstream saveFile; //Open standings txt file saveFile.open("dummy.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(3) << i->streakWin << setw(3) << i->streakLose << setw(2) << i->streakType << setw(3) << 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(); } //*********************************************************** // Function Name:printStandings // Purpose: To output to console all nodes of the created list // Parameters: None // Return Value:None // Data Members Accessed: struct Records member variables // Data Members Modified: None // Functions Called:list.begin(), list.end(), setw() //*********************************************************** 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; } } //*********************************************************** // Function Name: compareForSort // Purpose: Binary predicate to sort STL list // Parameters: // Return Value: // Data Members Accessed: // Data Members Modified: // Non-Local Variables Used: // Functions Called: //*********************************************************** bool Standings::compareForSort(const Records& node1, const Records& node2) { //list<Records>::iterator i; //for ( i=recordsList.begin(); i != recordsList.end(); ++i ) { //if return ((node1.totalWin > node2.totalWin) && (node1.winPercentage > node2.winPercentage) && (node1.division < node2.division)); //return true; //else //return false; //} }
•
•
Join Date: Nov 2009
Posts: 24
Reputation:
Solved Threads: 0
0
#15 27 Days Ago
So after more consideration I am thinking that my < operator isn't covering enough and that I need to cover more possibilities of what could happen something more like...
C++ Syntax (Toggle Plain Text)
friend bool operator < (Records& node1, Records& node2) { if (node1.division > node2.division){ if (node1.winPercentage > node2.winPercentage) return true; } else if (node1.division == node2.division){ if (node1.winPercentage > node2.winPercentage) return true; }else if(node1.division > node2.division){ if (node1.winPercentage == node2.winPercentage) return true; else if (node1.division == node2.division){ if (node1.winPercentage == node2.winPercentage) return true; } else return false; }
![]() |
Similar Threads
- passing STL list into a sort function (C++)
- c++ STL LIST function (C++)
- Problem accessing base class member function whilst iterating through STL list (C++)
- Using the STL LIst Container, how do I create, write,read, and store in file. (C++)
Other Threads in the C++ Forum
- Previous Thread: Functions,Pointers,Arrays = :@
- Next Thread: Sooo.... I need help...
| Thread Tools | Search this Thread |
api array arrays based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






