Board index » delphi » Why won't this compile

Why won't this compile

  I have been figurering that I should start putting some of the functions
I use into a unit. Well, last night I was doing just that, however when I
went to compile the unit I kept getting a complier error #3 Unknown
identifier from the function TFMAKE and TFOPEN. It says that the IFile is
the unknkown identifier. I even changed it to punk,junk, and just about
anything else you could think of. Kept getting the same old compile time
error. Anyone have any ideas as to what might be the problem?

Here is the unit that keeps giving me the error is below.

unit tfiles;
{ This unit checks and opens/appends/resets files. }

interface

function FTMake(Var TheFile   : text; FName : string) : boolean;
function FTOpen(Var TheFile   : text; FName : string) : boolean;
function FTAppend(Var TheFile : text; FName : string) : boolean;
function UTMake(Var TheFile : file; FName : string; ChunkSize : word) :
boolean;
function UTOpen(Var TheFile : file; FName : string; ChunkSize : word) :
boolean;
This> function TFMake(Var TheFile : IFile; FName : string) : boolean;
This> function TFOpen(Var TheFIle : IFile; FName : string) : boolean;

implementation

function FTMake(Var TheFile : text; Fname : string) : boolean;
{ Create a text file for output }
begin
  assign(TheFile, FName);
  {$I-}
  rewrite(TheFile);
  {$I+}
  if IOResult = 0 then
    FTMake := true
  else
    FTMake := false;
end;

function FTOpen(Var TheFile : text; FName : string) : boolean;
{ Opens an existing textfile for read operations }
begin
  assign(TheFile, FName);
  {$I-}
  reset(TheFile);
  {$I+}
  if IOResult = 0 then
    FTOpen := true
  else
    FTOpen := false;
end;

function FTAppend(Var TheFile : text; FName : string) : boolean;
{ Opens an existing text file for write operations }
begin
  assign(TheFile,FName);
  {$I-}
  append(TheFile);
  {$I+}
  if IOresult = 0 then
    FTAppend := true
  else
    FTAppend := false;
end;

function TFMake(Var TheFile : IFile ; FName : string) : boolean;
 { Creates a type file for output }
begin
  assign(TheFile,Fname);
  {$I-}
  rewrite(TheFile);
  {$I+}
  if IOResult = 0 then
    TFMake := true
  else
    TFMake := false;
end;

function TFOpen(Var TheFile : IFile; FName : string) : boolean;
 { Resets a typed file for In/Output work }
begin
  assign(TheFile,FName);
  {$I-}
  reset(TheFile);
  {$I+}
  if IOResult = 0 then
    TFOpen := true
  else
    TFOpen := false;
end;

function UTMake(Var TheFile : file; FName : string; ChunkSize : word) :
boolean;
 { Creates an Untyped file  }
begin
  assign(TheFile,FName);
  {$I-}
  rewrite(TheFile,ChunkSize);
  {$I+}
  if IOResult = 0 then
    UTMake := true
  else
    UTMake := false;
end;

function UTOpen(Var TheFile : file; FName : string; ChunkSize : word) :
boolean;
 { resets an UnTyped file ready for Output }
begin
  assign(TheFile,FName);
  {$I-}
  reset(TheFile,ChunkSize);
  {$I+}
  if IOResult = 0 then
    UTOpen := true
  else
    UTOpen := false;
end;
end.

 

Re:Why won't this compile


It's quite simple when you consider this: a Unit is like any Pascal
program: everything must be defined before it's referenced.  In the code
you posted, you reference data types (which are probably defined in the
main program), but this Unit doesn't know about your program - it knows
only what it has itself and in the Units it Uses.  Thus, becuase there
are data types referenced in your subprograms which haven't been defined
_in_ the Unit, the compiler can't resolve those data types.
   The fix is simple (and important to the use of Units): define global
data types (and variables) _in_ the Unit.  This will allow both the Unit
and programs which reference the Unit to "know" what the data types are
and resolve them as needed.  In applications with Units, you generally
want little data declared and types defined in the programs themselves -
the situation is best handled with a "global data" Unit which all Units
and programs reference.

Quote
>   I have been figurering that I should start putting some of the functions
> I use into a unit. Well, last night I was doing just that, however when I
> went to compile the unit I kept getting a complier error #3 Unknown
> identifier from the function TFMAKE and TFOPEN. It says that the IFile is
> the unknkown identifier. I even changed it to punk,junk, and just about
> anything else you could think of. Kept getting the same old compile time
> error. Anyone have any ideas as to what might be the problem?

> Here is the unit that keeps giving me the error is below.

> unit tfiles;
> { This unit checks and opens/appends/resets files. }

> interface

> function FTMake(Var TheFile   : text; FName : string) : boolean;
> function FTOpen(Var TheFile   : text; FName : string) : boolean;
> function FTAppend(Var TheFile : text; FName : string) : boolean;
> function UTMake(Var TheFile : file; FName : string; ChunkSize : word) :
> boolean;
> function UTOpen(Var TheFile : file; FName : string; ChunkSize : word) :
> boolean;
> This> function TFMake(Var TheFile : IFile; FName : string) : boolean;
> This> function TFOpen(Var TheFIle : IFile; FName : string) : boolean;

> implementation

> function FTMake(Var TheFile : text; Fname : string) : boolean;
> { Create a text file for output }
> begin
>   assign(TheFile, FName);
>   {$I-}
>   rewrite(TheFile);
>   {$I+}
>   if IOResult = 0 then
>     FTMake := true
>   else
>     FTMake := false;
> end;

> function FTOpen(Var TheFile : text; FName : string) : boolean;
> { Opens an existing textfile for read operations }
> begin
>   assign(TheFile, FName);
>   {$I-}
>   reset(TheFile);
>   {$I+}
>   if IOResult = 0 then
>     FTOpen := true
>   else
>     FTOpen := false;
> end;

> function FTAppend(Var TheFile : text; FName : string) : boolean;
> { Opens an existing text file for write operations }
> begin
>   assign(TheFile,FName);
>   {$I-}
>   append(TheFile);
>   {$I+}
>   if IOresult = 0 then
>     FTAppend := true
>   else
>     FTAppend := false;
> end;

> function TFMake(Var TheFile : IFile ; FName : string) : boolean;
>  { Creates a type file for output }
> begin
>   assign(TheFile,Fname);
>   {$I-}
>   rewrite(TheFile);
>   {$I+}
>   if IOResult = 0 then
>     TFMake := true
>   else
>     TFMake := false;
> end;

> function TFOpen(Var TheFile : IFile; FName : string) : boolean;
>  { Resets a typed file for In/Output work }
> begin
>   assign(TheFile,FName);
>   {$I-}
>   reset(TheFile);
>   {$I+}
>   if IOResult = 0 then
>     TFOpen := true
>   else
>     TFOpen := false;
> end;

> function UTMake(Var TheFile : file; FName : string; ChunkSize : word) :
> boolean;
>  { Creates an Untyped file  }
> begin
>   assign(TheFile,FName);
>   {$I-}
>   rewrite(TheFile,ChunkSize);
>   {$I+}
>   if IOResult = 0 then
>     UTMake := true
>   else
>     UTMake := false;
> end;

> function UTOpen(Var TheFile : file; FName : string; ChunkSize : word) :
> boolean;
>  { resets an UnTyped file ready for Output }
> begin
>   assign(TheFile,FName);
>   {$I-}
>   reset(TheFile,ChunkSize);
>   {$I+}
>   if IOResult = 0 then
>     UTOpen := true
>   else
>     UTOpen := false;
> end;
> end.

Re:Why won't this compile


Mike Mcgrane <mike...@primenet.com> wrote in article
<5g6k1q$...@nnrp1.news.primenet.com>...

Quote

Mike
these two functions are declared wrong, you have allowed an 'I' to creep
into your declaration.

function TFMake(Var TheFile : IFile; FName : string) : boolean;
function TFOpen(Var TheFIle : IFile; FName : string) : boolean;

they should then read
function TFMake(Var TheFile : File; FName : string) : boolean;
function TFOpen(Var TheFIle : File; FName : string) : boolean;

Glen

Re:Why won't this compile


w

Quote
>Mike
>these two functions are declared wrong, you have allowed an 'I' to creep
>into your declaration.

>function TFMake(Var TheFile : IFile; FName : string) : boolean;
>function TFOpen(Var TheFIle : IFile; FName : string) : boolean;

>they should then read
>function TFMake(Var TheFile : File; FName : string) : boolean;
>function TFOpen(Var TheFIle : File; FName : string) : boolean;

  Sorry, No I didn't.  There were declared properly as they were for a
typed file. That was the reasoning for TF, UT ect.. Typed file, UnTyped
file. Although now I understand why after reading Mike Copelands response
to my question. I just can't declare it like that with out the actual
structure being within the unit as I was wanting.

  -Mike

Other Threads