Eiffel继承和游标使用[无法编译]

时间:2017-10-23 16:02:16

标签: inheritance iterable eiffel

我有以下测试用例:

test_different_cursor: BOOLEAN
        local
            cursor: SET_ITERATION_CURSOR[INTEGER, INTEGER]
            sets: ARRAY[SET[INTEGER, INTEGER]]
        do
            create sets.make_empty

            check  attached {SET_ITERATION_CURSOR[INTEGER, INTEGER]} d.different_cursor as current_cursor then
                cursor := current_cursor
            end
            from
            until
                cursor.after
            loop
                sets.force (cursor.item, sets.count + 1)
                cursor.forth
            end

        end

这里,在~out的循环中,我尝试调用Array类的强制功能,但编译器继续说该行的跟随错误:

    Error code: VUAR(2)

Type error: non-compatible actual argument in feature call. 
What to do: make sure that type of actual argument is compatible with
  the type of corresponding formal argument. 

Class: MY_TESTS
Feature: test_different_cursor
Called feature: force (v: [like item] G; i: INTEGER_32) from ARRAY
Argument name: v
Argument position: 1
Formal argument type: SET [INTEGER_32, INTEGER_32]
Actual argument type: TUPLE [INTEGER_32, INTEGER_32]
Line: 193
        loop
->        sets.force (cursor.item, sets.count + 1)
          cursor.forth

在这种情况下,似乎我需要创建继承ARRAY类的SET类并在那里重新定义强制功能。但我不确定这是修复编译错误的正确方法。这是我的SET类的样子:

class
    SET[A, B]

create
    make

feature
    valueA: A
    valueB: B

feature
    make (first: A; second: B)
        do
            valueA := first
            valueB := second
        end
end

我需要做些什么来解决这个问题?

1 个答案:

答案 0 :(得分:0)

cursor.item返回的值需要转换为SET [INTEGER, INTEGER]。这可以通过添加局部变量

来完成
t: TUPLE [first: INTEGER; second: INTEGER]

到功能test_different_cursor并更改行

sets.force (cursor.item, sets.count + 1)

t := cursor.item
sets.force (create {SET [INTEGER, INTEGER]}.make (t.first, t.second), sets.count + 1)