944,135 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1272
  • C++ RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Nov 8th, 2009
0
Re: Questions on using the STL list sort method
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
Reputation Points: 10
Solved Threads: 0
Light Poster
Afupi is offline Offline
40 posts
since Nov 2009
Nov 8th, 2009
0
Re: Questions on using the STL list sort method
Post your code as of now.
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,865 posts
since Dec 2008
Nov 8th, 2009
0
Re: Questions on using the STL list sort method
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)
  1. #ifndef STANDINGS_H
  2. #define STANDINGS_H
  3.  
  4. #include <iostream>
  5. #include <list>
  6. #include <fstream>
  7. #include <iomanip>
  8.  
  9. using namespace std;
  10.  
  11. #define NAMELENGTH 20
  12.  
  13.  
  14. struct Records
  15. {
  16. int totalWin; //Each teams total wins
  17. int totalLose; //Each teams total loses
  18. int streakWin; //wins in last 10 games
  19. int streakLose; //loses in last 10 games
  20. int streakLength; //Number of wins or loses in
  21. //a row
  22. int homeWin; //number of games won at home
  23. int homeLose; //number of games lost at home
  24. int awayWin; //number of wins away
  25. int awayLose; //number of loses away
  26. int interWin; //Number of interleague games won
  27. int interLose;//Number of interleague games lost
  28. int division; //Which division each team is.
  29. double winPercentage; //Percentage of wins
  30. //compared to loses
  31. double gamesBehind; //Total number of
  32. // games played
  33. char teamName[NAMELENGTH]; //Team names
  34. char streakType; //If teams streak is winning or
  35. //losing
  36.  
  37. /*bool operator <(const Records& node)
  38. {
  39. return ((totalWin > node.totalWin) &&
  40. (winPercentage > node.winPercentage) &&
  41. (division < node.division));
  42. }*/
  43.  
  44. };
  45.  
  46.  
  47.  
  48. class Standings
  49. {
  50. private:
  51.  
  52. list<Records> recordsList; //List variable
  53. friend class Team; //Allows private member access
  54. //to the Team class
  55. public:
  56. //Constructor
  57. Standings();
  58. //Destructor
  59. ~Standings(){};
  60. //bool operator <(const Records&);
  61.  
  62. //Takes in list from txt file
  63. //and inserts it into the list
  64. void loadList();
  65. //Takes in the altered list and
  66. //saves to txt file
  67. void saveList();
  68. //Prints the list
  69. void printStandings();
  70. //Overloaded function to let sort
  71. //know how to sort list
  72. //Takes in two objects of the struct
  73. //Records
  74. bool compareForSort (const Records&, const Records&);
  75.  
  76. };
  77.  
  78. #endif
  79.  
  80. #include "standings.h"
  81.  
  82. //***********************************************************
  83. // Function Name:Standings
  84. // Purpose: Constructor
  85. //***********************************************************
  86. Standings::Standings()
  87. {
  88.  
  89. }
  90. /*
  91. bool Standings::operator <(const Records& node)
  92. {
  93. list<Records>::iterator i;
  94. //for ( i=recordsList.begin(); i != recordsList.end(); ++i ) {
  95. if ((i->totalWin > node.totalWin) && (i->winPercentage > node.winPercentage) && (i->division < node.division))
  96. return true;
  97. else
  98. return false;
  99.  
  100. //}
  101.  
  102.  
  103. }*/
  104.  
  105. //***********************************************************
  106. // Function Name: loadList
  107. // Purpose: To pull in info from a txt file and insert it into
  108. // a list.
  109. // Parameters: None
  110. // Return Value: None
  111. // Data Members Accessed: struct Records variables
  112. // Data Members Modified: recordsList
  113. // Functions Called: ifstream.open(), list.push_back(),
  114. // ifstream.close()
  115. //***********************************************************
  116. void Standings::loadList(){
  117. //Create a variable of ifstream type names loadFile
  118. ifstream loadFile;
  119. //Open standings txt file
  120. loadFile.open("dummy.txt");
  121. //Temporary object of struct type Record
  122. Records r;
  123.  
  124.  
  125. //Loop to take from file put into class
  126. //and than put class into list
  127. while(!loadFile.eof()){
  128. loadFile >> r.teamName
  129. >> r.totalWin
  130. >> r.totalLose
  131. >> r.winPercentage
  132. >> r.gamesBehind
  133. >> r.streakWin
  134. >> r.streakLose
  135. >> r.streakType
  136. >> r.streakLength
  137. >> r.homeWin
  138. >> r.homeLose
  139. >> r.awayWin
  140. >> r.awayLose
  141. >> r.interWin
  142. >> r.interLose
  143. >> r.division;
  144.  
  145. //Checks to make sure its not the end of file.
  146. //If its not the end of the file it inserts
  147. //the node at the end of the list
  148. if(!loadFile.eof())
  149. recordsList.push_back(r);
  150. }
  151. //closing the text file
  152. loadFile.close();
  153. }
  154.  
  155.  
  156. //***********************************************************
  157. // Function Name:saveList
  158. // Purpose: To put the updated list back into the original
  159. // text file
  160. // Parameters:None
  161. // Return Value: None
  162. // Data Members Accessed: struct Records member variables
  163. // Functions Called:list.sort(), ofstream.open(),
  164. // ofstream.close(), ofstream.flush(),
  165. // setw(), list.begin(), list.end()
  166. //***********************************************************
  167. void Standings::saveList()
  168. {
  169. //Sorts the List before saving to txt
  170. //recordsList.sort();
  171. recordsList.sort(&Standings::compareForSort);
  172. //Create a variable of ofstream type names saveFile
  173. ofstream saveFile;
  174. //Open standings txt file
  175. saveFile.open("dummy.txt");
  176. //Create an iterator to keep place of the nodes saved
  177. list<Records>::iterator i;
  178.  
  179. //Loop through each node and save each member to that node
  180. for ( i=recordsList.begin(); i != recordsList.end(); ++i ) {
  181. saveFile << right << setw(13) << i->teamName << setw(4) << i->totalWin
  182. << setw(4) << i->totalLose << setw(7) << setprecision(3) << i->winPercentage
  183. << setw(6) << i->gamesBehind << setw(3) << i->streakWin
  184. << setw(3) << i->streakLose << setw(2) << i->streakType
  185. << setw(3) << i->streakLength << setw(3)<< i->homeWin
  186. << setw(3) << i->homeLose << setw(3) << i->awayWin
  187. << setw(3) << i->awayLose << setw(3)<< i->interWin
  188. << setw(3) << i->interLose << setw(2) << i->division
  189. << endl;
  190. }
  191. //Flush and Close txt file
  192. saveFile.flush();
  193. saveFile.close();
  194.  
  195. }
  196.  
  197. //***********************************************************
  198. // Function Name:printStandings
  199. // Purpose: To output to console all nodes of the created list
  200. // Parameters: None
  201. // Return Value:None
  202. // Data Members Accessed: struct Records member variables
  203. // Data Members Modified: None
  204. // Functions Called:list.begin(), list.end(), setw()
  205. //***********************************************************
  206. void Standings::printStandings()
  207. {
  208. list<Records>::iterator i;
  209.  
  210. //Looping through each node and printing each variable of each node
  211. for ( i=recordsList.begin(); i != recordsList.end(); ++i )
  212. {
  213. cout << right << setw(13) << i->teamName << setw(4) << i->totalWin
  214. << setw(4) << i->totalLose << right << setw(7) << i->winPercentage
  215. << right << setw(6) << i->gamesBehind << right << setw(2) << i->streakWin
  216. << right << setw(2) << i->streakLose << right << setw(2) << i->streakType
  217. << right << setw(2) << i->streakLength <<right << setw(3)<< i->homeWin
  218. << right << setw(3) << i->homeLose << right << setw(3) << i->awayWin
  219. << right << setw(3) << i->awayLose << right << setw(3)<< i->interWin
  220. << right << setw(3) << i->interLose << right << setw(2) << i->division
  221. << endl;
  222. }
  223. }
  224.  
  225. //***********************************************************
  226. // Function Name: compareForSort
  227. // Purpose: Binary predicate to sort STL list
  228. // Parameters:
  229. // Return Value:
  230. // Data Members Accessed:
  231. // Data Members Modified:
  232. // Non-Local Variables Used:
  233. // Functions Called:
  234. //***********************************************************
  235. bool Standings::compareForSort(const Records& node1, const Records& node2)
  236. {
  237. //list<Records>::iterator i;
  238. //for ( i=recordsList.begin(); i != recordsList.end(); ++i ) {
  239. //if
  240. return ((node1.totalWin > node2.totalWin) && (node1.winPercentage > node2.winPercentage) && (node1.division < node2.division));
  241. //return true;
  242. //else
  243. //return false;
  244.  
  245. //}
  246.  
  247.  
  248. }
Reputation Points: 10
Solved Threads: 0
Light Poster
Afupi is offline Offline
40 posts
since Nov 2009
Nov 8th, 2009
0
Re: Questions on using the STL list sort method
Not sure where to go from here. Any suggestions?
Reputation Points: 10
Solved Threads: 0
Light Poster
Afupi is offline Offline
40 posts
since Nov 2009
Nov 9th, 2009
0
Re: Questions on using the STL list sort method
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)
  1. friend bool operator < (Records& node1, Records& node2)
  2. {
  3. if (node1.division > node2.division){
  4. if (node1.winPercentage > node2.winPercentage)
  5. return true;
  6. }
  7. else if (node1.division == node2.division){
  8. if (node1.winPercentage > node2.winPercentage)
  9. return true;
  10. }else if(node1.division > node2.division){
  11. if (node1.winPercentage == node2.winPercentage)
  12. return true;
  13. else if (node1.division == node2.division){
  14. if (node1.winPercentage == node2.winPercentage)
  15. return true;
  16. }
  17. else
  18. return false;
  19. }
Reputation Points: 10
Solved Threads: 0
Light Poster
Afupi is offline Offline
40 posts
since Nov 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Functions,Pointers,Arrays = :@
Next Thread in C++ Forum Timeline: Sooo.... I need help...





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC