Recent

Author Topic: [SOLVED] Abstract Generic Class uses wrong procedure  (Read 1075 times)

Jan_

  • New Member
  • *
  • Posts: 17
[SOLVED] Abstract Generic Class uses wrong procedure
« on: March 10, 2022, 05:15:28 am »
Hi,
i got the Runtime Error: Call to abstract method
Main file
Code: Pascal  [Select][+][-]
  1. program fluent_interfaces_1;
  2.  
  3. {$interfaces CORBA}
  4.  
  5. uses MapBuilder0_fluent , MapBuilder0Impl_fluent , MapBuilderAction_fluent ;
  6.  
  7. type
  8.  
  9.    TMapBuilder0Impl =specialize MapBuilder0Impl<String,Integer>;
  10.    TMapBuilderAction=specialize MapBuilderAction<String,Integer>;
  11.  
  12.   MapB = class(TMapBuilder0Impl)
  13.     constructor Create;
  14.   end;
  15.  
  16.    MapBuilderImpl = class(TMapBuilderAction )
  17.      public
  18.          procedure  put( key:String;  value:Integer); override;
  19.    end;
  20.  
  21.  
  22. //MapB
  23. Constructor MapB.Create;
  24. var
  25.   myaction:TMapBuilderAction;
  26. begin
  27.   myaction:=TMapBuilderAction.Create;
  28.   Inherited create(myaction);
  29. end;
  30.  
  31. //MapBuilderImpl
  32. procedure  MapBuilderImpl.put( key:String;  value:Integer);
  33. begin
  34.     WriteLn('PUT');
  35. end;
  36.  
  37. begin
  38.   MapB.Create().put('Hallo',0).put('Hallo',1);
  39.   readln();
  40. end.
  41.  
  42.  
  43.  
  44.  
MapBuilder0_fluent
Code: Pascal  [Select][+][-]
  1. unit MapBuilder0_fluent;
  2.  
  3. //generateIState
  4. {$interfaces CORBA}
  5.  
  6. interface
  7.  
  8.  
  9. type
  10.     generic MapBuilder0 <K, V>= interface
  11.   function put( key:K;  value:V):specialize MapBuilder0<K, V>;
  12. End;
  13. implementation
  14. End.
  15.  
MapBuilder0Impl_fluent
Code: Pascal  [Select][+][-]
  1. unit MapBuilder0Impl_fluent;
  2.  
  3. //generateState
  4. interface
  5. uses MapBuilderAction_fluent , MapBuilder0_fluent;
  6.  
  7. Type  
  8.   generic MapBuilder0Impl <K, V>= Class(TInterfacedObject,specialize MapBuilder0<K, V>)
  9.   private
  10.     action:specialize MapBuilderAction<K, V>;
  11.  
  12.   public
  13.     Constructor Create(myaction:specialize MapBuilderAction<K, V>);
  14.   function build():Integer;
  15.   procedure print();
  16.   function put( key:K;  value:V):specialize MapBuilder0<K, V>;
  17. End;
  18.  
  19. implementation
  20.  
  21. Constructor MapBuilder0Impl.Create(myaction:specialize MapBuilderAction<K, V>);
  22. Begin
  23.   Self.action := myaction;
  24. End;
  25.  
  26. function MapBuilder0Impl.put( key:K;  value:V):specialize MapBuilder0<K, V>;
  27. Begin
  28.       Self.action.state0_put(key, value);
  29.       exit( MapBuilder0Impl.Create(Self.action));
  30. End;
  31. End.
MapBuilderAction_fluent
Code: Pascal  [Select][+][-]
  1. unit MapBuilderAction_fluent;
  2.  
  3. //generateIAction
  4. interface
  5.  
  6. type
  7.   generic MapBuilderAction <K, V>= Class Abstract
  8.   procedure  state0_put( key:K;  value:V);
  9.   procedure  put( key:K;  value:V); virtual; abstract;
  10. End;
  11.  
  12. implementation
  13.  
  14. procedure  MapBuilderAction.state0_put( key:K;  value:V);
  15. Begin
  16.     put(key, value);
  17. End;
  18.  
  19. End.

in the procedure procedure  MapBuilderAction.state0_put( key:K;  value:V); i got the Error.
i do not see, why it not calls MapBuilderImpl.put( key:String;  value:Integer)
and how can i solve the Problem?
« Last Edit: March 10, 2022, 03:09:13 pm by Jan_ »

Zvoni

  • Hero Member
  • *****
  • Posts: 2330
Re: Abstract Generic Class uses wrong procedure
« Reply #1 on: March 10, 2022, 08:30:18 am »
??
https://www.freepascal.org/docs-html/ref/refse36.html
Quote
An abstract class is a class that cannot be instantiated directly. Instead, a descendent class must always be instantiated
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

Thaddy

  • Hero Member
  • *****
  • Posts: 14382
  • Sensorship about opinions does not belong here.
Re: Abstract Generic Class uses wrong procedure
« Reply #2 on: March 10, 2022, 08:34:29 am »
 procedure  state0_put( key:K;  value:V);virtual; // this one is not abstract, so for inheritance to work it should be virtual in first implementation?
 
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5486
  • Compiler Developer
Re: Abstract Generic Class uses wrong procedure
« Reply #3 on: March 10, 2022, 01:40:26 pm »
in the procedure procedure  MapBuilderAction.state0_put( key:K;  value:V); i got the Error.
i do not see, why it not calls MapBuilderImpl.put( key:String;  value:Integer)
and how can i solve the Problem?

In MapB.Create you do

Code: Pascal  [Select][+][-]
  1. myaction:=TMapBuilderAction.Create;

but instead it should be

Code: Pascal  [Select][+][-]
  1. myaction:=MapBuilderImpl.Create;

procedure  state0_put( key:K;  value:V);virtual; // this one is not abstract, so for inheritance to work it should be virtual in first implementation?

state0_put is fine as it is. It does not need to be virtual (at least not for the presented example).

Jan_

  • New Member
  • *
  • Posts: 17
Re: Abstract Generic Class uses wrong procedure
« Reply #4 on: March 10, 2022, 03:08:46 pm »
Realy big Thank Pascal Dragon.

 

TinyPortal © 2005-2018