Ada for "S" must be a variable

So, here is a piece of my body file. I get the error message "words.adb: 75: 42: relevant for" S "should be a variable".

procedure Remove_Character(S : in out Ustring; C : in Character; Successful : out Boolean) is begin for I in 1..length(S) loop if Element(S, I) = C then Delete(S, I, I); Successful := true; return; end if; end loop; Successful := false; end Remove_Character; function Is_Subset(Subset : Ustring; S : Ustring) return Boolean is Could_Remove : Boolean; begin for I in 1..length(Subset) loop Remove_Character(S , Element(Subset, I), Could_Remove); if Could_Remove = false then return false; end if; end loop; return True; end Is_Subset; 

I understand where my mistake comes from. Remove_Character uses S: in out Ustring, and the Is_Subset function uses S: in Ustring. My question is: how to change a variable from Remove_Character only in Ustring? Sorry if this got a little mixed up, I'm pretty new to programming and website.

+4
source share
1 answer

You cannot, at least not directly.

I do not know what UString , but I assume the Delete procedure modifies it. If you changed the S declaration in Remove_Character to S: in Ustring , you would probably get an error when calling Delete .

The easiest approach I can imagine is to make a copy of S in Is_Subset :

 Copy_Of_S: UString := S; 

and then pass the (modifiable) copy to Remove_Character .

By โ€œsimplest,โ€ I mean that it makes minimal change to your existing code. But you should probably consider reorganizing it. Determining whether one UString is a subset of the other by modifying one of the lines does not seem to be the best approach; I am sure there is a more efficient way to do this.

Small and irrelevant point: this:

 if Could_Remove = false then 

better to write like:

 if not Could_Remove then 
+5
source

Source: https://habr.com/ru/post/1481502/


All Articles