【问题标题】:Ada Actual for this must be a variable in out modeAda Actual 必须是 out 模式下的变量
【发布时间】:2012-10-30 03:55:53
【问题描述】:

我正在 Ada 中为数据结构和算法课程制作程序。

我目前的问题是错误'“this”的实际值必须是一个变量' 我环顾四周,我读到它是因为 in out 模式,但我并不完全理解为什么它会发生在我身上。

我看到的示例是有道理的,但我想因为这是我的编码,所以我只是没有看到它?

Procedure AddUnmarked(g:Grid; this:Linked_List_Coord.List_Type; c:Cell) is
      cNorth : Cell := getCell(g, North(c));
      cEast  : Cell := getCell(g, East(c));
      cSouth : Cell := getCell(g, South(c));
      cWest  : Cell := getCell(g, West(c));
   Begin
         if CellExists(g, cNorth) and not cNorth.IsMarked then
            Linked_List_Coord.Append(this, cNorth.Coords);
         elsif CellExists(g, cEast) and not cEast.IsMarked  then
            Linked_List_Coord.Append(this, cEast.Coords);
         elsif CellExists(g, cSouth) and not cSouth.IsMarked  then
            Linked_List_Coord.Append(this, cSouth.Coords);
         elsif CellExists(g, cWest) and not cWest.IsMarked  then
            Linked_List_Coord.Append(this, cWest.Coords);
         end if;
   End AddUnmarked;

在“this”被传递给函数之前,它是我自定义类型 Coord(2 个整数)的 Linked_List。在将列表传递给我的代码中的上述函数之前,它已被初始化并添加了一个坐标对。

【问题讨论】:

    标签: ada


    【解决方案1】:

    这意味着列表不能被修改,除非你将它作为可修改的参数传递,即in out

    更详细地说,将LIST_TYPE 视为标记类型对象的句柄;为了确保LIST_TYPE 有效,您需要通过in 参数(或创建/操作本地对象)将其传递,但​​要传递您的结果,您需要out 参数。

    因此,为了对已经存在的对象进行操作{并返回结果},您需要 in out

    【讨论】:

    • facepalm 谢谢你,我真的觉得自己智障哈哈。我参与了 List 的功能,但没有参与,所以从来没有想到这是个问题。
    • 没问题。我也详细阐述了一点。
    【解决方案2】:

    在 Ada 中,子程序参数都有与之相关的使用模式。可用模式为inoutin out*。如果你没有指定模式(就像你没有在你的代码中那样),那么它默认为 in only。

    模式指定您可以在子程序内部使用该参数做什么。如果要读取从例程外部传入的值,则它必须带有in。如果你想写入参数(和/或可能让它在例程之外读取),那么它必须有out

    由于您的所有参数都没有out,因此您无法写入任何参数。

    (* - 还有另一种可能的模式:access,但这是一个高级主题)。

    【讨论】:

    • 关于access参数模式的话题,有什么需要的吗?我知道 GNAT JVM Ada 编译器在导入的 [库] 规范中大量使用了它们。
    • @Shark8 - 如果您有这样的好问题,也许您应该将其作为 SE 问题提出。 :-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-15
    • 2011-10-31
    • 2012-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多