[fpc-pascal] Re: Use generic class as parameter (was: how to write enumerator for trees)

Xiangrong Fang xrfang at gmail.com
Fri Mar 22 02:27:52 CET 2013


Hi,

I missed the key point. This bug should be fixed in 2.7.1, but I am now
using 2.6.0.   Now there is another problem:

If I change my code to this:

      TTreapEnumerator = class
        FStack: TStack;
        FCurrent: PNode;
      public
*        constructor Create(root: PNode);*
        destructor Destroy; override;
        function MoveNext: Boolean;
        property Current: PNode read FCurrent;
      end;

The problem is gone, because PNode is not a generic class, but only a
record.   However, the following errors are generated:

Error: Forward declaration not solved "constructor
THistogram.TTreap$Char$LongInt.TTreapEnumerator.Create(PNode);"
Error: Forward declaration not solved "destructor
THistogram.TTreap$Char$LongInt.TTreapEnumerator.Destroy;"
Error: Forward declaration not solved
"THistogram.TTreap$Char$LongInt.TTreapEnumerator.MoveNext:Boolean;"

THistogram is defined as:

  THistogram = class(specialize TTreap<Char, Integer>)
  protected
    function DefaultTraverser(Key: Char; Value: Integer): Boolean; override;
    procedure DefaultDisposer(Value: Integer); override;
    function DefaultUpdater(Key: TKey; Value: TValue; IsNew: Boolean):
Boolean; override;
  public
    MKey: Char;
    Max: Integer;
    constructor Create; override;
  end;

I think there is no need to define TTreapEnumerator in THistogram again,
right?

Xiangrong


2013/3/22 Xiangrong Fang <xrfang at gmail.com>

> Hi,
>
> I try to do exactly the same as show here:
>
>
> QUOTE:
> http://svn.freepascal.org/svn/fpc/trunk/packages/fcl-stl/src/gvector.pp
>
> generic TVector<T> = class
>   ... ...
>   type
>     TVectorEnumerator = class
>     private
>       FVector: TVector;
>       FPosition: SizeUInt;
>       FFirstDone: Boolean;
>       function GetCurrent: T; inline;
>     public
>       *constructor Create(AVector: TVector);*
>       function MoveNext: Boolean; inline;
>       property Current: T read GetCurrent;
>     end;
>   ... ...
>   public
>
> My code:
>
> generic TTreap<TKey, TValue> = class
>   ... ...
>   type
>     TTreapEnumerator = class
>       FStack: TStack;
>       FCurrent: PNode;
>     public
>       *constructor Create(ATreap: TTreap);*
>
>
>       destructor Destroy; override;
>       function MoveNext: Boolean;
>       property Current: PNode read FCurrent;
>     end;
>   ... ...
>   private
>
> Error message I got is: Error: Generics without specialization cannot be used as a type for a variable
>
>
> Then I found this: http://bugs.freepascal.org/view.php?id=19499&history=1
>
> I wonder if this bug's fix is release or not? How does the TVector class compile with this problem?
>
> Thanks!
> Xiangrong
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20130322/d77c7f62/attachment.html>


More information about the fpc-pascal mailing list