Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't code from "The Tomes of Delphi" compile?

I'm trying to use the TDRecLst and TDSplyCm units from the code included with The Tomes of Delphi, but I get a compiler error in TDBasics.pas:

Identifier expected but 'CONST' found

I get a similar error in TDStrRes.inc:

Same error as above

What's wrong, and how do I fix it?

The code is available from the author.

like image 224
tbutton Avatar asked Jun 05 '13 20:06

tbutton


1 Answers

You're evidently using a Delphi version that's newer than Delphi 6. Despite being updated in 2005, the code from that book only detects up to that version of Delphi. TDDefine.inc defines a number of compiler symbols based on the version it detects, but when the version you're using isn't anything it recognizes, it defines no symbols. That eventually leads to problems later when the compiler encounters code like this in TDBasics.pas;

implementation

uses
  {$IFDEF Delphi1}
  WinTypes, WinProcs;
  {$ENDIF}
  {$IFDEF Delphi2Plus}
  Windows;
  {$ENDIF}
  {$IFDEF Kylix1Plus}
  Types, Libc;
  {$ENDIF}

{$IFDEF Delphi1}
{$R TDStrRes.r16}
{$ENDIF}
{$IFDEF Delphi2Plus}
{$R TDStrRes.r32}
{$ENDIF}
{$IFDEF Kylix1Plus}
{$R TDStrRes.r32}
{$ENDIF}

const
  UnitName = 'TDBasics';

Since none of Delphi1, Delphi2Plus, or Kylix1Plus is defined, the uses clause is empty. When we ignore all the compiler directives and inactive code blocks, the compiler ultimately sees code like this:

implementation

uses

const
  UnitName = 'TDBasics';

That's why the compiler complains about expecting an identifier instead of const.

To fix it, you need to teach TDDefine.inc to recognize your version of Delphi. Easier, though, might be to ignore all the version-detection code and hard-code all the symbols that apply to the version you're using. As long as you never use any version older than Delphi 6, all the symbols will apply to all your versions.

Find the following block of code in TDDefine.pas:

{$IFDEF VER140}
  {$DEFINE Delphi6}
  {$DEFINE Delphi1Plus}
  {$DEFINE Delphi2Plus}
  {$DEFINE Delphi3Plus}
  {$DEFINE Delphi4Plus}
  {$DEFINE Delphi5Plus}
  {$DEFINE Delphi6Plus}
  {$DEFINE HasAssert}
{$ENDIF}

Remove the first and last lines so that the remaining $DEFINE instructions are processed unconditionally.

like image 86
Rob Kennedy Avatar answered Oct 22 '22 16:10

Rob Kennedy