[fpc-pascal]Linked List/ pointers/ casting/ OOP Question

Marcel Martin mm10 at ellipsa.net
Sat Jul 10 01:48:03 CEST 2004


Ron Weidner a écrit :
> 
> I thought I was getting closer to understanding
> pointers but it appears that I'm still not getting it.
>  I wrote the following code fragment but it doesn't
> compile.  the errors I get are...
> 
> Widget_container_class.pas(80,27) Error: Illegal
> qualifier
> Widget_container_class.pas(88,23) Error: Illegal
> qualifier
> Widget_container_class.pas(88,29) Error: Illegal
> expression
> Widget_container_class.pas(88,29) Fatal: Syntax error,
> ";" expected but "(" found
> 
> I numbered lines 80 and 88 in comments so that the
> error messages make sense.  (to the experienced
> programmer like yourself :) )Please let me know if you
> see what the problems are.
> 
> {$H+}
> unit Widget_container_class;
> interface
> uses
> Attribute_class,Widget_class,Widget_entity_class,web;
> type PWidgetList=^TWidgetList;
> TWidgetList=record
>         widget:pointer;
>         next:PWidgetList;
> end;
> 
> type PWidgetContainer=^TWidgetContainer;
> TWidgetContainer=Object(TWidget)
>         private
>                 PCurWidget,PHeadWidget:PWidgetList;
>                 text:string;
>         public
>                 constructor init(ATag:string);
>                 procedure Render();
>                 procedure SetText(str:string);
>                 function GetText():string;
>                 procedure MoveFirstWidget();
>                 procedure MoveLastWidget();
>                 procedure AddWidget(PWidget:pointer);
>                 procedure RenderWidgets();
> end;
> 
> procedure TWidgetContainer.RenderWidgets();
> begin
>         MoveFirstWidget();
>         while (PCurWidget^.next <> nil) do
>         begin
>                 PCurWidget^.widget:=PWidget(PCurWidget^.widget); //
> cast the pointer to a widget pointer

The previous line is useless. It just sets widget with the value it 
already has and it does not change the fact that widget is an untyped 
pointer.

You should rather use
          if PWidget(PCurWidget^.widget)^.meta = 'ENTITY' then 

instead of the following line

>                 if (PCurWidget^.widget^.meta = 'ENTITY') then //
> this is line 80
>                 begin
> 
> PCurWidget^.widget:=PWidgetEntity(PCurWidget^.widget);
>                 end
>                 else
>                 begin
> 
> PCurWidget^.widget:=PWidgetContainer(PCurWidget^.widget);
>                 end;
>                 PCurWidget^.widget^.Render(); //line 88

idem, use    PWidget(PCurWidget^.widget)^.Render();

>                 PCurWidget:=PCurWidget^.next;
>                 dispose(PHeadWidget);
>                 PHeadWidget:=PCurWidget;
>         end;
> end;

-- 
mm
http://www.ellipsa.net/




More information about the fpc-pascal mailing list