[fpc-pascal]Linked List/ pointers/ casting/ OOP Question
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 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 PCurWidget:=PCurWidget^.next; dispose(PHeadWidget); PHeadWidget:=PCurWidget; end; end; Ron_W __ Do you Yahoo!? New and Improved Yahoo! Mail - Send 10MB messages! http://promotions.yahoo.com/new_mail ___ fpc-pascal maillist - [EMAIL PROTECTED] http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal]Linked List/ pointers/ casting/ OOP Question
On Fri, 9 Jul 2004, Ron Weidner wrote: > 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 > if (PCurWidget^.widget^.meta = 'ENTITY') then // this is line 80 At first glance, this should read PwidgetSomething(PCurWidget^.widget)^.meta = 'ENTITY' because widget is an untyped pointer ? And the same for the other line ? Michael. ___ fpc-pascal maillist - [EMAIL PROTECTED] http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal]Linked List/ pointers/ casting/ OOP Question
> At first glance, this should read > PwidgetSomething(PCurWidget^.widget)^.meta = > 'ENTITY' > because widget is an untyped pointer ? > > And the same for the other line ? > > Michael. Thanks Michael. Actually widget is a typed pointer but the problem I was trying to solve is that widget could be one of two different types. I think I solved my problem with the following code, but I will need write a test program to be sure. It compiles so I must be getting close. :) procedure TWidgetContainer.RenderWidgets(); var entity:PWidgetEntity; container:PWidgetContainer; base:PWidget; begin MoveFirstWidget(); while (PCurWidget^.next <> nil) do begin base:=PCurWidget^.widget; // cast the pointer to a widget pointer if (base^.meta = 'ENTITY') then begin PCurWidget^.widget:=PCurWidget^.widget; entity:=PCurWidget^.widget; entity^.Render(); end else begin container:=PCurWidget^.widget; container^.Render(); end; PCurWidget:=PCurWidget^.next; dispose(PHeadWidget); PHeadWidget:=PCurWidget; end; end; Ron_W __ Do you Yahoo!? New and Improved Yahoo! Mail - Send 10MB messages! http://promotions.yahoo.com/new_mail ___ fpc-pascal maillist - [EMAIL PROTECTED] http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal]Linked List/ pointers/ casting/ OOP Question
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, usePWidget(PCurWidget^.widget)^.Render(); > PCurWidget:=PCurWidget^.next; > dispose(PHeadWidget); > PHeadWidget:=PCurWidget; > end; > end; -- mm http://www.ellipsa.net/ ___ fpc-pascal maillist - [EMAIL PROTECTED] http://lists.freepascal.org/mailman/listinfo/fpc-pascal
[fpc-pascal]Yet another problem concerning Qualifiers
Hello List! Compiling the unit === unit test; interface procedure test.do_test; implementation procedure test.do_test; begin end; end. === results in compiler errors: test.pas(5,15) Error: overloaded identifier TEST isn't a function test.pas(5,15) Fatal: Syntax error, ; expected but . found IMHO, this is not the correct behaviour. Note: I still use 1.0.10. Thank you, Anton. ___ fpc-pascal maillist - [EMAIL PROTECTED] http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal]Linked List/ pointers/ casting/ OOP Question
Hello! Ron Weidner wrote: 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 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 PCurWidget:=PCurWidget^.next; dispose(PHeadWidget); PHeadWidget:=PCurWidget; end; end; Ron_W __ Do you Yahoo!? New and Improved Yahoo! Mail - Send 10MB messages! http://promotions.yahoo.com/new_mail ___ fpc-pascal maillist - [EMAIL PROTECTED] http://lists.freepascal.org/mailman/listinfo/fpc-pascal The type cast actually does nothing at runtime, so the line PCurWidget^.widget:=PWidget(PCurWidget^.widget); // does nothing. Type casts are just to inform the compiler, at compile time, to accept a type for some variable or constant it would otherwise reject. So, you have to typecast where you use it, i. e. -everywhere- you use it. Instead of: PCurWidget^.widget:=PWidget(PCurWidget^.widget); // if (PCurWidget^.widget^.meta = 'ENTITY') then // try: if (PWidget(PCurWidget^.widget)^.meta = 'ENTITY') then // Note: I don't see the definition of PWidget in your code peace. I assume it's a pointer to a record containing a string member named meta. But in your procedure procedure AddWidget(PWidget:pointer); you use the very same identifier, PWidget, for a variable name. That's a bit confusing. hth, Anton. ___ fpc-pascal maillist - [EMAIL PROTECTED] http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal]Yet another problem concerning Qualifiers
Anton Tichawa a écrit : > > Hello List! > > Compiling the unit > > === > > unit test; > > interface > > procedure test.do_test; > > implementation > > procedure test.do_test; > begin > end; > > end. > > === > > results in compiler errors: > > test.pas(5,15) Error: overloaded identifier TEST isn't a function > test.pas(5,15) Fatal: Syntax error, ; expected but . found And without the "test."? -- mm http://www.ellipsa.net/ [EMAIL PROTECTED] ( suppress no.sp.am. ) ___ fpc-pascal maillist - [EMAIL PROTECTED] http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal]Linked List/ pointers/ casting/ OOP Question
> Note: I don't see the definition of PWidget in your > code peace. I assume > it's a pointer to a record containing a string > member named meta. But in > your procedure > > procedure AddWidget(PWidget:pointer); > > you use the very same identifier, PWidget, for a > variable name. That's a > bit confusing. You are the 2nd person that I've talked to that seemed confused about this. Since I'm very new to pascal programming, I'm concerned that I may have a bad design. In an attempt to confirm a design problem I'm going to try to explain what I'm trying to do and ask for advice on a better way to do it. Ok... Basically I have 3 objects that look like this... Widget ---|-- | | Container WidgetEntity Widget One of the defining differances between an Entity Widget and a Container widget is that a Container Widget can contain other widgets. (both other container widgets and entity widgets). So I needed to create a dynamic list (linked list) that could address either a container widget or an entity widget. So I created TWidgetList with the field 'widget' being an untyped pointer. Now that I have this list, I need to add addresses of _existing_ widgets to the list. So, when I call the method... procedure AddWidget(PWidget:pointer); I'm referring to a PWidget but I don't know of which type. (container or entity) During construction of an Entity or Container widget, the string meta is set to identify what type of widget it is. Now later, I can traverse the TWidgetList, read the meta string, cast the widget pointer to the right type and call the correct object methods. (Render() in this case.) Is my code the long way around the barn or does it seem to make better sense explained? Ron_W __ Do you Yahoo!? Read only the mail you want - Yahoo! Mail SpamGuard. http://promotions.yahoo.com/new_mail ___ fpc-pascal maillist - [EMAIL PROTECTED] http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal]Yet another problem concerning Qualifiers
--- Marcel Martin <[EMAIL PROTECTED]> wrote: > Anton Tichawa a écrit : > > > > Hello List! > > > > Compiling the unit > > > > === > > > > unit test; > > > > interface > > > > procedure test.do_test; > > > > implementation > > > > procedure test.do_test; > > begin > > end; > > > > end. > > > > === > > > > results in compiler errors: > > > > test.pas(5,15) Error: overloaded identifier TEST > isn't a function > > test.pas(5,15) Fatal: Syntax error, ; expected but > . found The following compiled for me... unit test; interface procedure do_test(); implementation procedure do_test(); begin end; begin end. I think you only need to fully qualify methods (functions or procedures) when they are part of an object. Ron_W __ Do you Yahoo!? Yahoo! Mail - 50x more storage than other providers! http://promotions.yahoo.com/new_mail ___ fpc-pascal maillist - [EMAIL PROTECTED] http://lists.freepascal.org/mailman/listinfo/fpc-pascal