From: int376f@aurora.cc.monash.edu.au (Jamie Scuglia)
Subject: HELP NEEDED - INHERITING A LIST AND SORTING IT - C++
Organization: Monash University
Date: Wed, 26 Aug 1992 12:32:37 GMT


Hi!  I have a small problem with some C++ code I am writing, to handle
a football tipping competion.  I have a ladder structure, which is to
hold ladders (positions) of participants in the tipping competition, as well
as a ladder of the football teams.  My basic class structure consists
of 3 classes for the ladder system.  

- A base class, called LADDER, which defines the basic attributes
  of a ladder, including a definition for a simple linked list, and
  it also has a member function to sort the ladder.

- A derived class called TEAM_LADDER which includes some more attributes
  related to football team statistics that a ladder holds.

- A second derived class (from LADDER), called PLAYER_LADDER, which
  has a more specilized attributes relating to player performance in
  the tipping competition.

The set-up of these classes looks like this: (with '....' = other stuff)
------------------------------------------------------------------------
class LADDER
{
   protected:
	char name[60];
	int points;
	float percentage;
	LADDER *next_entry;
	LADDER *last_entry; 

   public:
	LADDER(char n[], int pts, float p, LADDER *next, LADDER* last);
	virtual LADDER *Sort_ladder(LADDER *ladder);
};

class PLAYER_LADDER : public LADDER
{	
   private:
	char login_name[12]; 

   public:
	........
	void Save_ladder(PLAYER_LADDER *ladder);
	int Compare(PLAYER_LADDER *lad1, PLAYER_LADDER *lad2);
	.........
};

class TEAM_LADDER : public LADDER
{
   private:
      float match_ratio;
      int games_played;
      ......

   public:
      ......
      void Save_ladder(TEAM_LADDER *ladder);
      int Compare(TEAM_LADDER *lad1, TEAM_LADDER *lad2);
      ......
};
------------------------------------------------------------------------

Now, the problem occurs in the function PLAYER_LADDER:Save_ladder,
(and TEAM_LADDER::Save_ladder).
To save the ladder, I need to traverse the list I inherited from
the base class LADDER.  *BUT*, I can't do that (the compiler complains)
because in Save_ladder there is this code:

        PLAYER_LADDER *player_ladder;
        ......
        player_ladder = player_ladder->next;  /* next ladder entry */

which is no good, because the "next" pointer in PLAYER_LADDER actually
points to the base class LADDER, not PLAYER_LADDER as I wanted it to, 
even though I inherited the basic list structure from the class LADDER.
So, the compiler tells me it can't assign a type PLAYER_LADDER to LADDER
when I try to traverse the list that I inherited.  And I can't change the
"player_ladder" pointer to type LADDER (base-class) because then I would
lose the "login_name" field I added to PLAYER_LADDER.

Any simple ideas to overcome that?

You also might notice that I have a "Compare" function in
PLAYER_LADDER and TEAM_LADDER classes to compare two ladder entries
to see if they are in the correct order.  Since a TEAM_LADDER and
a PLAYER_LADDER are sorted according to different criteria, I wrote
a version for each of these 2 classes.  Now, the function Sort_ladder
in the base class has an awful lot of trouble finding the correct Compare
function.  Note that the Sort_ladder function in the base class
is called with a PLAYER_LADDER or a TEAM_LADDER pointer during
the execution of the program (the whole idea was to write one
sorting function to sort both a TEAM_LADDER and a PLAYER_LADDER - 
with the Compare function being called in the Sort_ladder function
to determine whether the ordering was correct, or whether
entries need to be moved).  So, either a pointer to PLAYER_LADDER
or TEAM_LADDER is passed to the sorting routine, and I would
like it to somehow know where to find the appropriate Comapare
function (depending on whether a PLAYER_LADDER or TEAM_LADDER
was passed to the sorting routine).  I don't know whether
what I want is possible, but in summary, I need a system
to sort two different ladders, using mostly the same code.  

Thank-you for taking the time to read all of this.  I eagerly
await any ideas you may have out there.  If anything is unclear
above, please feel free to ask for clarification.

  ___                        ()                _
 (   >                       /\               //
  __/___.  ______  o _      /  )  _. . . _,  // o __.
 / / (_/|_/ / / <_<_</_    /__/__(__(_/_(_)_</_<_(_/|_
<_/                                      /|
                                        |/
3rd Year Student at Monash University, Melbourne, Victoria, Australia.
* E-mail: int376f@aurora.cc.monash.edu.au
	  jamie@yoyo.cc.monash.edu.au