访问HASH_TABLE元素埃菲尔

时间:2014-01-04 12:07:42

标签: hashtable eiffel

我有这个简单的代码,我想在ARRAYED_SET中访问HASH_TABLE中的一个元素,但是我收到一个错误:

Error: target of the Object_call might be void.
What to do: ensure target of the call is attached.

这是我的代码:

class
APPLICATION

inherit
    ARGUMENTS

create
    make

feature {NONE}
make
local

    elem: ARRAYED_SET[INTEGER]
    table: HASH_TABLE[ARRAYED_SET[INTEGER], INTEGER]
do
    create elem.make (3)
    create table.make (1)

    elem.put (4)
    table.put (elem, 1)

    table.at (1).go_i_th (1)
end
end

1 个答案:

答案 0 :(得分:2)

当您访问HASH_TABLE中的某个项目时,该项目可能不存在。因此,此功能的签名是

item (k: K): detachable G

如果找不到该项,则返回Void(或扩展类型的默认值)。因此,当您尝试使用HASH_TABLE中的项目时,应检查它是否已附加。这可以通过替换:

来实现
table.at (1).go_i_th (1)

使用:

if attached table.at (1) as la_element then
  la_element.go_i_th (1)
end