【问题标题】:What is that the Error of Illegal assignment and how to correct it?那是什么错误的非法分配以及如何纠正它?
【发布时间】:2020-09-25 12:58:44
【问题描述】:
procedure tri_selection(t: tab; n: Integer);
var
 i, j, min, aux: Integer;
begin

  for i := 1 to n - 1 do
  begin

    min := i;

    for j := i + 1 to n do
      if t[j] < t[min] then
        j := min;

    if min <> i then
    begin
      aux := t[i];
      t[i] := t[min];
      t[min] := aux;
    end;

  end;

end;

这应该是一个正确且众所周知的代码,用于将整数从下到上排列,但编译器仍然坚持说“非法分配给 for loop 'j' 变量”。

有什么问题?

【问题讨论】:

    标签: pascal freepascal turbo-pascal


    【解决方案1】:

    问题出在这里:

    for j := i + 1 to n do
      if t[j] < t[min] then
        j := min;                      // <-- Not allowed to assign to FOR loop variable j
    

    您不能分配给for 循环变量。

    也许你打算写

    for j := i + 1 to n do
      if t[j] < t[min] then
        min := j;
    

    【讨论】:

      【解决方案2】:

      你忘记了过程标题中 t 之前的 var

      【讨论】:

      • 如果您在过程参数列表中澄清为什么在t 之前需要var,这可能是一个很好的答案。
      猜你喜欢
      • 2019-02-10
      • 1970-01-01
      • 2013-10-13
      • 2018-01-27
      • 2022-08-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-11
      相关资源
      最近更新 更多