| | |
Questions on using the STL list sort method
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2009
Posts: 22
Reputation:
Solved Threads: 0
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.
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.
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; 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; } }
0
#2 27 Days Ago
You cannot use std::sort with list (if that is what you intended to do)
list has its own sort. Here is an example : http://www.cplusplus.com/reference/stl/list/sort/
list has its own sort. Here is an example : http://www.cplusplus.com/reference/stl/list/sort/
1) What word becomes shorter if you add a letter to it? [ Solved by : niek_e, Paul Thompson] 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: 22
Reputation:
Solved Threads: 0
0
#3 27 Days Ago
Line 102 is where I call sort. recordsList is the name of my list, and compareForSort is the name of the function I wrote Though I am sure I need help with that function as well. Maybe I am trying to use std::sort but I thought recordsList.sort(compareForSort) was calling the list specific sort. Btw cplusplus is one of the places I did read about creating the compare function.
0
#5 27 Days Ago
C++ Syntax (Toggle Plain Text)
recordsList.sort(compareForSort);
Try this :
C++ Syntax (Toggle Plain Text)
recordsList.sort(&Standings::compareForSort);
Last edited by firstPerson; 27 Days Ago at 6:39 pm.
1) What word becomes shorter if you add a letter to it? [ Solved by : niek_e, Paul Thompson] 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: 22
Reputation:
Solved Threads: 0
0
#7 27 Days Ago
Ok now I am just getting
xutility(348) : error C2064: term does not evaluate to a function taking 2 arguments
I assume it is talking about the binary predicate function, but everywhere I read the binary predicate in other peoples example has two arguments.
Does it have to do with the fact that The nodes in the list are of struct Records while the list is being created in the standings class? Though the variables in Records are public so I dont see why that would be an issue. Here is the predicate function I have currently
xutility(348) : error C2064: term does not evaluate to a function taking 2 arguments
I assume it is talking about the binary predicate function, but everywhere I read the binary predicate in other peoples example has two arguments.
Does it have to do with the fact that The nodes in the list are of struct Records while the list is being created in the standings class? Though the variables in Records are public so I dont see why that would be an issue. Here is the predicate function I have currently
C++ Syntax (Toggle Plain Text)
bool Standings::compareForSort(const Records& node1, const Records& node2) { return ((node1.totalWin > node2.totalWin) && (node1.winPercentage > node2.winPercentage) && (node1.division < node2.division)); }
•
•
Join Date: Nov 2009
Posts: 22
Reputation:
Solved Threads: 0
0
#8 27 Days Ago
After doing more reading I found that some people use an overloaded < operator. So I attempted this and still getting loads of errors.
error C2676: binary '<' : 'Records' does not define this operator or a conversion to a type acceptable to the predefined operator
Here is my overloaded <
error C2676: binary '<' : 'Records' does not define this operator or a conversion to a type acceptable to the predefined operator
Here is my overloaded <
C++ Syntax (Toggle Plain Text)
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;
•
•
Join Date: Nov 2009
Posts: 22
Reputation:
Solved Threads: 0
0
#9 27 Days Ago
I am going to continue putting the things I try because I dont want anyone to think I am not trying for myself. So I tried putting the overloaded < operator within my struct Records. No errors. The program is running fine. Except when it is time to sort its not sorting anything. So I guess it could be the logic or syntax within the operator
EDIT: Is there another way to sort a struct list by multiple variable values?
EDIT: Is there another way to sort a struct list by multiple variable values?
C++ Syntax (Toggle Plain Text)
bool operator <(const Records& node) { return ((totalWin > node.totalWin) && (winPercentage > node.winPercentage) && (division < node.division)); }
Last edited by Afupi; 27 Days Ago at 1:06 am.
0
#10 27 Days Ago
>>xutility(348) : error C2064: term does not evaluate to a function taking 2 arguments
Which line is this from?
Which line is this from?
1) What word becomes shorter if you add a letter to it? [ Solved by : niek_e, Paul Thompson] 2) What does this sequence equal to : (.5u - .5a)(.5u-.5b)(.5u-.5c) ...[*] [*solved by : murtan] 3) What is the 123456789 prime numer?
![]() |
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 binary c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game generator givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






