Error with derived class function calls.

Started by monkey0506, Mon 29/08/2005 02:09:58

Previous topic - Next topic

monkey0506

Okay, the subject pretty much says what the problem is...so let's jump straight into the code:

Code: ags
#define ScrollingDialog_MAX_DLGS 500
#define ScrollingDialog_MAX_OPTS 30
#define ScrollingDialog_NUM_LBLS 7

enum ScrollingDialogOptionState {
  eScrollingOptionOn,
  eScrollingOptionOff,
  eScrollingOptionOffByScrolling,
  eScrollingOptionOffForever
  };

struct ScrollingDialog {
  protected ScrollingDialogOptionState State[ScrollingDialog_MAX_OPTS];
  protected int ID;
  import void Run(int prevdlg = -1);
  /* a lot of other stuff */
  };

struct ScrollingDialogRunning extends ScrollingDialog {
  protected int Option[ScrollingDialog_NUM_LBLS];
  /* other stuff */
  };

void ScrollingDialog::Run(int prevdlg) {
  /* setup dialog function */
  if (this != RunningDialog) RunningDialog.Run(this.ID);
  else {
    if ((prevdlg < 0) || (prevdlg > ScrollingDialog_MAX_DLGS))
      AbortGame("Invalid call to function 'ScrollingDialog::Run'.  Check the read me file for details.");
    int i = 0;
    int j = 0;
    while (i < ScrollingDialog_MAX_OPTS) {
      if (j < ScrollingDialog_NUM_LBLS) {
        if (this.State[i] == eScrollingOptionOn) this.Option[j] = i; /* HEREIN LIES THE PROBLEM */
        j++;
        }
      /* store running dialog vars */
      i++;
      }
    this.ID = prevdlg;
    while (j < ScrollingDialog_NUM_LBLS) {
      this.Option[j] = -1;
      j++;
      }
    return;
    }
  /* set dialog options on GUI */
  }


Okay, if you read the line if (this.State == eScrollingOptionOn) this.Option[j] = i; /* HEREIN LIES THE PROBLEM */ you will see that I tried to access the Option array of the RunningDialog object (as defined previously).  I know that I am working with RunningDialog because of the initial testing if (this != RunningDialog) RunningDialog.Run(this.ID);.

When I try to compile it, I get an error saying that Option is not a public member of ScrollingDialog.  ARG!!!

Any help greatly appreciated.

EDIT:  3 edits later, and I actually included all the info needed to understand all my variables and things.  Yay!

EDIT:  I figured it was worth a shot to try declaring ScrollingDialogRunning::Run(int prevdlg) separately, but then I got an error to the effect of "ScrollingDialogRunning does not contain a function Run".  Meh.

Gilbert

Since the code is in the "else" part, that means (this==RunningDialog) why don't you just use:
if (this.State == eScrollingOptionOn) RunningDialog.Option[j] = i; /* HEREIN LIES THE PROBLEM */
?

I'm not familiar with (i.e. know nothing about) these modern oo-technogarbages, but I think it's quite obvious as Option is not a member of ScrollingDialog, but a member of ScrollingDialogRunning (which is an extension of ScrollingDialog). Now Run is a function member of ScrollingDialog, it probably can't take care with any members which are just defined in the extension, if the compiler let it to pass it could generate more problems when there're something wrong in your code which makes it try to access Option[] when using just a ScrollingDialog object.

monkey0506

I understand why there could be problems if it did allow it, but the Option array is protected, meaning that it can't be accessed directly.  At the moment it can only be accessed using the keyword this.  And I don't want to make the array public (accessible directly) because then the user could corrupt the array (by changing the values).  This array is supposed to store the ID of the shown dialog options, and if the user edits it, it could cause a lot of problems for my Script Module... :-\

Kweepa

Don't expose the structure at all.
Just give the user static functions - it's less confusing that way.
Still waiting for Purity of the Surf II

Gilbert

If you're afraid of that, just to complicate things further, can you define another member function of ScrollingDialogRunning which modifies Option[]? Like:

RunningDialog.SetOption(num, i);

I know it's basically the same thing as directly accessing Option[], but I think people will just make less mistakes by calling a fuction than directly modifying a variable.

I personally don't care about this thing, and I'll just make everything public, protected or private stuff are useless to me.

monkey0506

#5
That's an interesting idea...however I would have to create a HUGE array to do that.  Let's see...200 * 30 * 500...3 million?  Hmm...and then iterating through that to get/set the text...possible now with the noloopcheck keyword...but...I'd prefer a different alternative.  I really would.  I'm already having to iterate through 6000 chars as it is...

Although I may go ahead and give that a go...for now...

Hmm...

State[15000];
HalfState[15000];
Text[3000000];
HalfText[3000000];

Boy this is going to be fun.


And Gilbot, I don't want the user to be able to change it at all...which is the problem.

EDIT:  Nevermind.  I was being stupid.  I understand perfectly now. :D

Pumaman

AGS only supports very basic inheritance -- there's no polymorphism so you can't override the method in a derived class.

However, the error you're getting makes sense, you cannot access an attribute of a derived class from the base class, that's standard OO rules.

Is there any need to use inheritance? Just move the Option array into the ScrollingDialog class is the easiest solution.

monkey0506

Yeah, I suppose that would work...it just doesn't make sense for every dialog to have one when it's only needed for the RunningDialog.  Thanks Pumaman. :)

SMF spam blocked by CleanTalk