2

次のコードで「定数式が必要です」というエラーが表示されます。

TBoardと定義されている:

  TBoard = class
    field: array[1..5,1..5] of Integer;

    function check(const x, y: Integer): Integer;
    function addShip(x, y, size, dir: Integer): Integer;
    function attack(const x, y: Integer): Integer;
  end;

マークされた行でエラーが発生します。

function TBoard.attack(const x, y: Integer): Integer;
begin
  Result := Self.check(x, y);
  case Result of
  0:
    Self.field[x, y] := 1;
    Exit; // error: constant expression expected
  else Exit;
  end;
end;

誰かが何が起こっているのか知っていますか?
前もって感謝します!

4

1 に答える 1

10

case ステートメント内に begin と end がないだけなので、関数を次のように変更します。

function TBoard.attack(const x, y: Integer): Integer;
begin
  Result := Self.check(x, y);
  case Result of
  0:
    begin
      Self.field[x, y] := 1;
      Exit; 
    end
  else Exit;
  end;
end;

ただし、これが完全なコードである場合は、非常に単純化できます。これらすべての終了は必要なく、case ステートメントも必要ありません。

function TBoard.attack(const x, y: Integer): Integer;
begin
  Result := Self.check(x, y);
  if Result = 0 then
    Self.field[x, y] := 1;
end;
于 2016-02-20T13:34:42.313 に答える