gpt4 book ai didi

procedure - 如何修复: 'Argument of Type Conversion Must Be Single Expression' in Ada 95

转载 作者:行者123 更新时间:2023-12-02 23:28:02 26 4
gpt4 key购买 nike

我正在 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 个字符

你必须决定你想要什么表现形式来让你在单个角色中区分红色国王和黑色国王。或者,更好的是,接受您需要 2 个角色来代表您的作品。或者,也许您可​​以使用单个字符,并遵循以下约定:小写值无冠,大写值有冠,空格表示未被占用。

“类型转换参数”错误的原因是,在类似的行中

   Checkers_Board(I, J) := Checkers_String(Char_Count);

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

关于procedure - 如何修复: 'Argument of Type Conversion Must Be Single Expression' in Ada 95,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55428712/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com