I am trying to make a CONTAINER class that maintains an array of CRITTER objects (that I have already created and tested. Note, there are various CRITTER subspecies, that are inheriting from the CRITTER super class). The aim is to add and remove CRITTER objects from this array. This is what the CONTAINER class looks like:
class CONTAINER create make feature num: detachable INTEGER list: ARRAY[CRITTER] make local do create list.make_empty num := 0 end addCritter(critter: CRITTER) do list.put(animal, num) num := num + 1 end removeCritter(critter: CRITTER) do list.put (list.at (num), ???) -- put last element in position of element to be removed list.remove_tail (num) -- remove tail num := num - 1 end end
Firstly, I can instantiate the CONTAINER class inside APPLICATION, but when I call
create container.make container.addCritter(myCritter)
I get a precondition, invalid index violation error on the second line. This may be because I have not set the upper and lower bounds of the array. However, when I try to do so, I get syntax errors. Which is the way to solve this issue?
Secondly, in order to remove an object from the array, it would help if I could get hold of the index value, but I can’t see any function that does this, unless I am missing something.