2017-10-22 2 views
0

바이트의 배열 [0..2]이 있습니다. 바이트 배열에 있는지 여부를 확인해야합니다. 그러나 if ($52 in byteArray) then을 사용할 때 "Operator is overloaded"오류가 발생합니다. 추가 변수를 바이트로 설정 한 다음 문에서 사용했지만 여전히 오류가 발생했습니다. 다음을 보여주는 매우 간단한 프로그램이 있습니다 :바이트 배열에서 한 바이트를 확인할 때 "연산자가 오버로드되지 않았습니다."

program overloaded; 

var 
    byteArray: array[0..2] of Byte; 

begin 
    byteArray[0] := $00; 
    byteArray[1] := $69; 
    byteArray[2] := $52; 

    if ($52 in byteArray) then 
    writeLn('We will not get to this point'); 
end. 

위의 오류와 함께 컴파일하는 데 실패합니다.

+4

예상됩니다. 배열을 반복해야합니다. –

답변

0

두 가지 대안이 있습니다. 연산자를 오버로딩하거나 for를 사용하십시오.

program ByteArrayIteration; 
var 
    ByteArray: array[0..2] of Byte = ($00, $69, $52); 
    SomeByte : Byte = $52; 
begin 
    for SomeByte in byteArray do 
    if SomeByte = $52 then 
    begin 
     WriteLn('We will get to this point'); 
     Break; 
    end; 
end. 

그리고 과부하 대안 :

program OverloadInOperator; 

uses SysUtils; 

operator in(const A: Byte; const B: TBytes): Boolean; 
var 
    i: Integer; 
begin 
    Result := True; 
    for i := Low(B) to High(B) do if A = B[i] then Exit; 
    Result := False; 
end; 

var 
    Bytes : TBytes; 
    AByte : Byte = $52; 
begin 
    Bytes := TBytes.Create($41, $52, $90); 
    if AByte in Bytes then WriteLn('Done'); 
end. 

사용 사례는 어레이 당 255 개 항목으로 제한되어있는 경우, 대신 세트를 사용하는 것이 좋습니다.

program SetOfByte; 
var 
    Bytes : set of Byte = [$41, $52, $90]; 
    AByte : Byte = $52; 
begin 
    if AByte in Bytes then WriteLn('Done'); 
end. 
관련 문제