如何修复:Ada 95 中的 'Argument of Type Conversion Must Be Single Expression'

How to fix: 'Argument of Type Conversion Must Be Single Expression' in Ada 95

我正在用 Ada95 编写一个简单的程序来检查棋盘布局输入是否有效。我对 Ada 很陌生,我知道我有很多东西要学。 我不断收到的错误消息是:

35:31: argument of type conversion must be single expression

47:07: argument of type conversion must be single expression

47:22: illegal operand for array conversion

53:10: argument of type conversion must be single expression

58:10: argument of type conversion must be single expression

61:13: argument of type conversion must be single expression

64:13: argument of type conversion must be single expression

66:13: argument of type conversion must be single expression

68:13: argument of type conversion must be single expression

76:15: invalid use of subtype mark in expression or call

我在下面包含了我的完整代码,这样如果有任何可能导致声明出现问题的地方,它可供参考。

-- RULES:
-- Only black squares may have pieces on them
-- Black squares start at the bottom left hand corner
-- Total checker count for a color cannot exceed 12
-- A color may not have more than 7 kings

with Ada.Text_IO, Ada.Integer_Text_IO;
use Ada.Text_IO, Ada.Integer_Text_IO;

procedure Checker_Checker is
  type Checkers_Piece is ("b", "BK", "RK", "BC", "RC");
  type Checkers_Board is array (1..8, 1..8) of Checkers_Piece;
  Checkers_String: String(1..64);
  invalid, 
    BK_Count, -- black king checker
    RK_Count, -- red king checker
    RC_Count, -- red checker
    BC_Count, -- black checker
    b_Count, -- blank space
    Char_Count      : Integer := 0;
  
  procedure Print_Board (Object: in Checkers_Board) is
    Print_Divider: String(1..17);
    Print_Squares: String(1..17);
  begin
    Print_Divider := "-----------------";
    Print_Squares := "| | | | | | | | |";
    for X in 1..8 loop
      Put_Line(Print_Divider);
      for Y in 1..8 loop
        Print_Squares(Y+1) := Checkers_Board(X, Y);
      end loop;
      Put_Line(Print_Squares);
    end loop;
    Put_Line(Print_Divider);
  end Print_Board;
  
begin
  Get(Checkers_String);
  for I in 1..8 loop
    for J in 1..8 loop
      Char_Count := Char_Count + 1;
      Checkers_Board(I, J) := Checkers_String(Char_Count);
    end loop;
  end loop;
  
  for Y in 1..8 loop
    for X in 1..8 loop
      if Checkers_Board(Y, X) = 'b' then
        Put_Line("There is a piece on a white space.");
        invalid := invalid + 1;
      end if;

      if Checkers_Board(Y, X) = "BK" then
        BK_Count := BK_Count + 1;
        BC_Count := BC_Count + 1;
      elsif Checkers_Board(Y, X) = "RK" then
        RK_Count := RK_Count + 1;
        RC_Count := RC_Count + 1;
      elsif Checkers_Board(Y, X) = "RC" then
        RC_Count := RC_Count + 1;
      elsif Checkers_Board(Y, X) = "BC" then
        BC_Count := BC_Count + 1;
      elsif Checkers_Board(Y, X) = "b" then
        b_Count := b_Count + 1;
      else
        Put_Line("There is an  unidentified character on the board.");
      end if;
    end loop;
  end loop;
  
  Print_Board;
  
  if RK_Count > 7 then
    Put_Line("There are more than 7 Red Kings on the board.");
  end if;
  
  if BK_Count > 7 then
    Put_Line("There are more than 7 Black Kings on the board.");
  end if;
  
  if RC_Count > 12 then
    Put_Line("There are more than 12 Red Checkers on the board.");
  end if;
  
  if BC_Count > 12 then
    Put_Line("There are more than 12 Black Checkers on the board.");
  end if;
  
  if b_Count < 32 then
    Put_Line("There are too many checkers on the board.");
  end if;
  
  if invalid = 0 then
    Put_Line("This is a valid checker board.");
  else
    Put_Line("This is an invalid checker board.");
  end if;
end Checker_Checker;

我知道我的代码很乱..请不要批评它..我正在尽我最大的努力学习 Ada。 谢谢你的帮助。

首先,

type Checkers_Piece is ("b", "BK", "RK", "BC", "RC");

正在尝试声明一个枚举;但枚举文字是标识符,而不是字符串。 所以应该是

   type Checkers_Piece is (b, BK, RK, BC, RC);

然后,在

    Print_Squares := "| | | | | | | | |";
    for X in 1..8 loop
      Put_Line(Print_Divider);
      for Y in 1..8 loop
        Print_Squares(Y+1) := Checkers_Board(X, Y);
      end loop;
      Put_Line(Print_Squares);
    end loop;

您显然希望 Checkers_Board(X, Y) 是一个 Character,但在您的声明中您希望它是一种 String,最多 2 个 Character ]s.

你必须决定你想要什么样的表示法来区分单个字符中的红色国王和黑色国王。或者,更好的是,接受您需要 2 个字符来代表您的作品。或者您可以使用单个 Characters,约定小写值不加冕,大写加冕,space 表示未占用。

你的 'argument of type conversion' 错误的原因是,像

这样的行
   Checkers_Board(I, J) := Checkers_String(Char_Count);

Checkers_Board是一个类型;您在这里需要该类型的对象,而不是类型本身。您使用的语法用于类型转换 (ARM 4.6)。