Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Failure to Inherit

I'm not sure what the problem is with my code, so if anyone can spot the error I'd be grateful.

There are two files involved. Here is a mock source code for file #1:

unit Test;

type
Yadda = class(TObject)
procedure X;
end;

implementation

procedure Yadda.X;
begin
end;

begin
end.

--------------------------------------
File #2

Program Example;

uses Test;

type
YaddaYadda = class(Yadda)
procedure X; override;

--------------------------------------

That's as far as the compiler will go. I get an error message saying YaddaYadda has no ancestor class to override. What is wrong?!

Comments

  • ManningManning Member Posts: 1,621
    : I'm not sure what the problem is with my code, so if anyone can spot the error I'd be grateful.
    :
    : There are two files involved. Here is a mock source code for file #1:
    :
    : unit Test;
    :
    : type
    : Yadda = class(TObject)
    : procedure X;
    : end;
    :
    : implementation
    :
    : procedure Yadda.X;
    : begin
    : end;
    :
    : begin
    : end.
    :
    : --------------------------------------
    : File #2
    :
    : Program Example;
    :
    : uses Test;
    :
    : type
    : YaddaYadda = class(Yadda)
    : procedure X; override;
    :
    : --------------------------------------
    :
    : That's as far as the compiler will go. I get an error message saying YaddaYadda has no ancestor class to override. What is wrong?!

    Where was procedure X declared in the Yadda class? If it is private, it won't be seen in the class that inherits it, so it can't be overridden.
  • zibadianzibadian Member Posts: 6,349
    : I'm not sure what the problem is with my code, so if anyone can spot the error I'd be grateful.
    :
    : There are two files involved. Here is a mock source code for file #1:
    :
    : unit Test;
    :
    : type
    : Yadda = class(TObject)
    : procedure X;
    : end;
    :
    : implementation
    :
    : procedure Yadda.X;
    : begin
    : end;
    :
    : begin
    : end.
    :
    : --------------------------------------
    : File #2
    :
    : Program Example;
    :
    : uses Test;
    :
    : type
    : YaddaYadda = class(Yadda)
    : procedure X; override;
    :
    : --------------------------------------
    :
    : That's as far as the compiler will go. I get an error message saying YaddaYadda has no ancestor class to override. What is wrong?!
    :
    Only virtual and dynamic methods can be overridden. See the help files for more info on the virtual and dynamic directives. In this case I would suggest, that you make the method virtual.
  • UGuardianUGuardian Member Posts: 27
    Well, I've tried making the procedure public and virtual with no result. The compiler still claims there is no ancestor class.

    What makes this really annoying is that if the base class and the derived class are both in the same unit, it works fine.

    There must be some way to create a new class from a class in another unit and override some of the methods.
  • zibadianzibadian Member Posts: 6,349
    : Well, I've tried making the procedure public and virtual with no result. The compiler still claims there is no ancestor class.
    :
    : What makes this really annoying is that if the base class and the derived class are both in the same unit, it works fine.
    :
    : There must be some way to create a new class from a class in another unit and override some of the methods.
    :
    Then you need to specify that the procedure is either public or protected. Here is a code sample:
    [code]
    unit Unit1;

    interface

    uses
    Unit1;

    type
    TYadda = class(TObject)
    private
    protected
    public
    procedure X; virtual;
    end;
    [/code]
    [code]
    unit Unit2;

    interface

    uses
    Unit1;

    type
    TYaddaYadda = class(TYadda)
    private
    protected
    public
    procedure X; override;
    end;
    [/code]
    This will work, since it's something which I code on a regular bases.
  • UGuardianUGuardian Member Posts: 27
    When that code didn't work I tried something a little more drastic. I deleted the compiler & installed a fresh copy.

    NOW the code is working. Looks like the compiler got corrupted somewhere along the line, thus it malfunctioned.

    Seesh. Thanks for trying though!
Sign In or Register to comment.