简体   繁体   中英

Error: No overloaded versoin of 'IntToStr'

When compiling the following code:

procedure TMainWin.FormActivate(Sender: TObject);
  var LineRaw : String;
  LinesFile : TextFile;
  i, i2 : integer;
  tempChar : String;
  CurTempCharPos : integer;
begin
  AssignFile(LinesFile, 'Lines.txt');
  Reset(LinesFile);
  i := 0;
  tempChar := '';
  CurTempCharPos := 1;

  while not EoF(LinesFile) do begin
    i := i+1; //ticker
    ReadLn(LinesFile, LineRaw);
    for i2 := 0 to 4 do begin
      tempChar := LineRaw[CurTempCharPos] + LineRaw[CurTempCharPos +1];
      Lines[i,i2] := IntToStr(tempChar);
      tempChar := '';
      CurTempCharPos := CurTempCharPos + 3;
    end;

  end;

  CloseFile(LinesFile);
end;

With Lines being defined in another form:

unit uGlobal;

interface

  type
    aLines = array[1..5] of integer;
    aLinesFinal = array of aLines;

  var
    Lines : aLinesFinal;

implementation

end.

I get the following error: There is no overloaded version of 'IntToStr' that can be called with these arguments. The error points to the line:

Lines[i,i2] := IntToStr(tempChar);

Here is the declaration of tempChar :

tempChar : String;

It is a string. And here is the call that the compiler rejects:

Lines[i,i2] := IntToStr(tempChar);

The IntToStr function, which has various overloads, accepts integer input parameters and returns strings. You cannot pass a string to IntToStr . Perhaps you meant to write:

Lines[i,i2] := StrToInt(tempChar);

Some other comments:

  • I doesn't look like you initialised Lines . This means that whilst the code might compile, it will fail at runtime.
  • Since you declared aLines as array[1..5] of integer , the valid values for i2 are 1 to 5 inclusive. You use 0 to 4 inclusive. Again, that's going to bite at runtime.
  • You really should enable range checking as a matter of urgency, since when you start executing this code that setting will reveal the errors above, and no doubt more besides.
  • In my view tempChar is a poor name for something that can hold more than a single character.
  • As @TLama points out, OnActivate seems to be an unusual place to execute this code. This event will run multiple times. Perhaps you should be executing this code at start up. In any case, code like this should not be in an event handler and should be moved to a separate method which an event handler can call.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM