home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / cplus / 12908 < prev    next >
Encoding:
Text File  |  1992-08-26  |  4.8 KB  |  125 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!munnari.oz.au!bruce.cs.monash.edu.au!monu6!aurora.cc.monash.edu.au!int376f
  3. From: int376f@aurora.cc.monash.edu.au (Jamie Scuglia)
  4. Subject: HELP NEEDED - INHERITING A LIST AND SORTING IT - C++
  5. Message-ID: <1992Aug26.123237.29846@monu6.cc.monash.edu.au>
  6. Sender: news@monu6.cc.monash.edu.au (Usenet system)
  7. Organization: Monash University
  8. Date: Wed, 26 Aug 1992 12:32:37 GMT
  9. Lines: 114
  10.  
  11.  
  12. Hi!  I have a small problem with some C++ code I am writing, to handle
  13. a football tipping competion.  I have a ladder structure, which is to
  14. hold ladders (positions) of participants in the tipping competition, as well
  15. as a ladder of the football teams.  My basic class structure consists
  16. of 3 classes for the ladder system.  
  17.  
  18. - A base class, called LADDER, which defines the basic attributes
  19.   of a ladder, including a definition for a simple linked list, and
  20.   it also has a member function to sort the ladder.
  21.  
  22. - A derived class called TEAM_LADDER which includes some more attributes
  23.   related to football team statistics that a ladder holds.
  24.  
  25. - A second derived class (from LADDER), called PLAYER_LADDER, which
  26.   has a more specilized attributes relating to player performance in
  27.   the tipping competition.
  28.  
  29. The set-up of these classes looks like this: (with '....' = other stuff)
  30. ------------------------------------------------------------------------
  31. class LADDER
  32. {
  33.    protected:
  34.     char name[60];
  35.     int points;
  36.     float percentage;
  37.     LADDER *next_entry;
  38.     LADDER *last_entry; 
  39.  
  40.    public:
  41.     LADDER(char n[], int pts, float p, LADDER *next, LADDER* last);
  42.     virtual LADDER *Sort_ladder(LADDER *ladder);
  43. };
  44.  
  45. class PLAYER_LADDER : public LADDER
  46. {    
  47.    private:
  48.     char login_name[12]; 
  49.  
  50.    public:
  51.     ........
  52.     void Save_ladder(PLAYER_LADDER *ladder);
  53.     int Compare(PLAYER_LADDER *lad1, PLAYER_LADDER *lad2);
  54.     .........
  55. };
  56.  
  57. class TEAM_LADDER : public LADDER
  58. {
  59.    private:
  60.       float match_ratio;
  61.       int games_played;
  62.       ......
  63.  
  64.    public:
  65.       ......
  66.       void Save_ladder(TEAM_LADDER *ladder);
  67.       int Compare(TEAM_LADDER *lad1, TEAM_LADDER *lad2);
  68.       ......
  69. };
  70. ------------------------------------------------------------------------
  71.  
  72. Now, the problem occurs in the function PLAYER_LADDER:Save_ladder,
  73. (and TEAM_LADDER::Save_ladder).
  74. To save the ladder, I need to traverse the list I inherited from
  75. the base class LADDER.  *BUT*, I can't do that (the compiler complains)
  76. because in Save_ladder there is this code:
  77.  
  78.         PLAYER_LADDER *player_ladder;
  79.         ......
  80.         player_ladder = player_ladder->next;  /* next ladder entry */
  81.  
  82. which is no good, because the "next" pointer in PLAYER_LADDER actually
  83. points to the base class LADDER, not PLAYER_LADDER as I wanted it to, 
  84. even though I inherited the basic list structure from the class LADDER.
  85. So, the compiler tells me it can't assign a type PLAYER_LADDER to LADDER
  86. when I try to traverse the list that I inherited.  And I can't change the
  87. "player_ladder" pointer to type LADDER (base-class) because then I would
  88. lose the "login_name" field I added to PLAYER_LADDER.
  89.  
  90. Any simple ideas to overcome that?
  91.  
  92. You also might notice that I have a "Compare" function in
  93. PLAYER_LADDER and TEAM_LADDER classes to compare two ladder entries
  94. to see if they are in the correct order.  Since a TEAM_LADDER and
  95. a PLAYER_LADDER are sorted according to different criteria, I wrote
  96. a version for each of these 2 classes.  Now, the function Sort_ladder
  97. in the base class has an awful lot of trouble finding the correct Compare
  98. function.  Note that the Sort_ladder function in the base class
  99. is called with a PLAYER_LADDER or a TEAM_LADDER pointer during
  100. the execution of the program (the whole idea was to write one
  101. sorting function to sort both a TEAM_LADDER and a PLAYER_LADDER - 
  102. with the Compare function being called in the Sort_ladder function
  103. to determine whether the ordering was correct, or whether
  104. entries need to be moved).  So, either a pointer to PLAYER_LADDER
  105. or TEAM_LADDER is passed to the sorting routine, and I would
  106. like it to somehow know where to find the appropriate Comapare
  107. function (depending on whether a PLAYER_LADDER or TEAM_LADDER
  108. was passed to the sorting routine).  I don't know whether
  109. what I want is possible, but in summary, I need a system
  110. to sort two different ladders, using mostly the same code.  
  111.  
  112. Thank-you for taking the time to read all of this.  I eagerly
  113. await any ideas you may have out there.  If anything is unclear
  114. above, please feel free to ask for clarification.
  115.  
  116.   ___                        ()                _
  117.  (   >                       /\               //
  118.   __/___.  ______  o _      /  )  _. . . _,  // o __.
  119.  / / (_/|_/ / / <_<_</_    /__/__(__(_/_(_)_</_<_(_/|_
  120. <_/                                      /|
  121.                                         |/
  122. 3rd Year Student at Monash University, Melbourne, Victoria, Australia.
  123. * E-mail: int376f@aurora.cc.monash.edu.au
  124.       jamie@yoyo.cc.monash.edu.au
  125.