Operatore non sovraccarico

Sep 10 2020

Buon giorno, sto facendo degli esercizi di Codeforces nel mio tempo libero e ho avuto problemi a verificare se l'utente era un ragazzo o una ragazza, beh, il mio problema non è quello, ho appena dimostrato il codice. Durante la compilazione del codice nel mio computer (sto usando la versione 3.0.4 per i386) non ricevo alcun errore, ma codeforces mi dà questo errore

program.pas(15,16) Error: Operator is not overloaded: "freq(Char;AnsiString):LongInt;" + "ShortInt"
program.pas(46,4) Fatal: There were 1 errors compiling module, stopping 

L'errore non era abbastanza chiaro per me, poiché lo stesso script era perfettamente compilato con la mia versione. La piattaforma sta utilizzando (versione 3.0.2 i386-Win32).

program A236;
uses wincrt, sysutils;

var
    username : String;


function freq(char: char; username : String): Integer;
var 
   i: Integer;
begin
    freq:= 0;
    for i:= 1 to length(username) do
        if char = username[i] then
            freq:= freq + 1;
            //writeln(freq);        
end; 

function OddUserName(username : String): Boolean;
var
    i, counter: Integer;

begin
    OddUserName:= false; // even
    counter:= 0;
    for i:= 1 to length(username) do 
        if freq(username[i], username) <> 1 then    
            delete(username, i, 1)
        else
            counter:= counter + 1;  
    if counter mod 2 <> 0 then
        OddUserName:= true; // odd
    //writeln(counter); 
    //writeln(OddUserName);                 
end;        

begin
    readln(username);
    if not OddUserName(username) then 
        writeln('CHAT WITH HER!')
    else 
        writeln('IGNORE HIM!'); 


    //readkey();    
end.

Probabilmente l'errore dovrebbe essere su questa riga:

function freq(character: char; username : String): Integer;

Grazie per tutti coloro che aiutano.

Risposte

1 RemyLebeau Sep 10 2020 at 15:26

All'interno di una funzione, il nome della funzione può essere utilizzato come sostituto dell'utilizzo di una variabile locale esplicita o Result. freq()e lo OddUserName()stanno facendo entrambi, ma freq()utilizza solo il nome della funzione come operando sul lato destro di un'assegnazione. freq := freq + 1; dovrebbe essere una dichiarazione legale nei moderni compilatori Pascal, vedere Perché posso usare il nome della funzione in pascal come nome della variabile senza definizione? .

Tuttavia, sembrerebbe che il messaggio di errore suggerisca che il compilatore in errore tratti freql'istruzione freg + 1come un tipo di funzione e non come una variabile locale. Questo spiegherebbe perché si lamenta di non essere in grado di aggiungere un ShortIntcon un tipo di funzione.

Quindi, dovrai invece usare una variabile locale esplicita, (o la Resultvariabile speciale , se il tuo compilatore lo fornisce), ad esempio:

function freq(charToFind: char; username : String): Integer;
var 
  i, f: Integer;
begin
  f := 0;
  for i := 1 to Length(username) do
    if charToFind = username[i] then
      f := f + 1;
      //writeln(f);
  freq := f;
end; 
function freq(charToFind: char; username : String): Integer;
var 
  i: Integer;
begin
  Result := 0;
  for i := 1 to Length(username) do
    if charToFind = username[i] then
      Result := Result + 1;
      //writeln(f);
end;