Recent

Author Topic: Why does this not compile  (Read 5818 times)

Yogi

  • New Member
  • *
  • Posts: 42
Why does this not compile
« on: October 12, 2010, 06:42:41 pm »
Can anybody give me a hint or the reason why the following does not compile?
 :-[
Code: [Select]
program Project1;

{$mode objfpc}{$H+}

uses
  {$IFDEF UNIX}{$IFDEF UseCThreads}
  cthreads,
  {$ENDIF}{$ENDIF}
  Classes
  { you can add units after this };

type

  { tbase1 }

  tbase1 = class
    public
      constructor create(const aname:string); virtual;
  end;

  { tbase2 }

  tbase2 = class(tbase1)
    public
      constructor create(const aname:string); override;
      constructor create(xxx:integer);
  end;

  { tbase3 }

  tbase3 = class(tbase2)
    public
      constructor create(const aname:string); override;
  end;

{ tbase1 }

constructor tbase1.create(const aname:string);
begin
end;

{ tbase2 }

constructor tbase2.create(const aname:string);
begin
  inherited create(aname);
end;

constructor tbase2.create(xxx:integer);
begin
end;

{ tbase3 }

constructor tbase3.create(const aname:string);
begin
  inherited create(aname);
end;

var
  b1 : tbase1;
  b2 : tbase2;
  b3 : tbase3;

begin
  b1:=tbase1.create('Test'); // ok
  b2:=tbase2.create('Test'); // ok
  b2:=tbase2.create(123); // ok
  b3:=tbase3.create('Test');  // ok
  b3:=tbase3.create(123); // project1.lpr(70,24) Error: Incompatible type for arg no. 1: Got "ShortInt", expected "AnsiString"
end.

Why can tbase3 not use the public constructor for integer from tbase2?

I came to this when I derived a class from TMemIniFile and wanted to use the
constructor for reading the ini-file from stream and not from file. From there I got this
type of error and I tried to create a example (above) for better showing.

I think as the "integer"-constructor from tbase2 is public and therefore visible
it should be possible to use it for tbase3 or should it not?
Lazarus 1.3 SVN 44197 * FPC 2.7.1 SVN 26822
Linux: 3.2.0-4-686-pae * Debian wheezy
Windows: W98

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1932
Re: Why does this not compile
« Reply #1 on: October 12, 2010, 07:00:50 pm »
I don't know exaclty what is going on. But I can say that it compiles here, if you mark the constructors in tbase2 with the overload modifier:

Code: [Select]
  tbase2 = class(tbase1)
    public
      constructor create(const aname:string); override; overload;
      constructor create(xxx:integer); overload;
  end;


JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4597
  • I like bugs.
Re: Why does this not compile
« Reply #2 on: October 12, 2010, 07:02:26 pm »
Methods with the same name but different parameter signature should have "overload; after their definition.
It is little confusing (resembles "override;") and I don't see much reason why it must be there.

Juha
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1932
Re: Why does this not compile
« Reply #3 on: October 12, 2010, 07:16:31 pm »
@Juha: Yes, but it's strange that it compiles, unless you use it in tbase3.
This behaviour also doesn't really match with the description here afaics:
http://www.freepascal.org/docs-html/ref/refsu66.html

Yogi

  • New Member
  • *
  • Posts: 42
Re: Why does this not compile
« Reply #4 on: October 12, 2010, 07:26:25 pm »
Thanks for fast reaction

from: inifiles.pp

Code: [Select]

TIniFile = class(TCustomIniFile)
   .
   .
   .
  public
    constructor Create(const AFileName: string; AEscapeLineFeeds : Boolean = False); override;
    constructor Create(AStream: TStream; AEscapeLineFeeds : Boolean = False);
   

From Your answer then the second constructor for the TIniFile-Class of the fcl should have "overload"

Should I give it as a feature wish to the bugtracker or is it fpc related?
Lazarus 1.3 SVN 44197 * FPC 2.7.1 SVN 26822
Linux: 3.2.0-4-686-pae * Debian wheezy
Windows: W98

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1932
Re: Why does this not compile
« Reply #5 on: October 12, 2010, 07:29:19 pm »
Should I give it as a feature wish to the bugtracker or is it fpc related?

I'd ask the people on the fpc-devel mailing-list first:
http://www.freepascal.org/maillist.var

 

TinyPortal © 2005-2018