问题描述
早上好,我在业余时间做一些Codeforces练习,并且在测试用户是男孩还是女孩时遇到问题,嗯,我的问题不是,我只是演示了代码.在计算机上编译我的代码时(我在i386上使用3.0.4版本),我没有收到任何错误,但是Codeforces给了我这个错误
Good day, I'm doing some Codeforces exercises in my free time, and I had a problem to test if the user was a boy or a girl, well, my problem isn't that, i have just demonstrated the code.While compiling my code in my computer ( I'm using version 3.0.4 for i386 ) i get no error, but codeforces gives me this error
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
这个错误对我来说还不够清楚,因为同一脚本已完美地与我的版本一起编译了.该平台正在使用(版本3.0.2 i386-Win32).
The error wasn't clear enough to me, as the same script was perfectly compiled with my version.The platform is using ( version 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.
错误可能在此行:
function freq(character: char; username : String): Integer;
感谢所有提供帮助的人.
Thanks for everyone who helps.
推荐答案
在函数内部,该函数的名称可以替代使用显式局部变量或Result
. freq()
和OddUserName()
都这样做,但是只有freq()
使用函数名作为赋值右侧的操作数. freq := freq + 1;
应该是现代Pascal编译器中的法律声明,请参见为什么我可以在pascal中使用函数名作为变量名称没有定义?.
Inside of a function, the function's name can be used as a substitute for using an explicit local variable or Result
. freq()
and OddUserName()
are both doing that, but only freq()
is using the function name as an operand on the right-hand side of an assignment. freq := freq + 1;
should be a legal statement in modern Pascal compilers, see Why i can use function name in pascal as variable name without definition?.
但是,错误消息似乎表明失败的编译器正在将语句freg + 1
中的freq
视为函数类型,而不是局部变量.那可以解释为什么它抱怨不能添加具有函数类型的ShortInt
.
However, it would seem the error message is suggesting that the failing compiler is treating freq
in the statement freg + 1
as a function type and not as a local variable. That would explain why it is complaining about not being able to add a ShortInt
with a function type.
因此,您将不得不使用显式的局部变量(或,如果编译器提供的话,则使用特殊的Result
变量),例如:
So, you will have to use an explicit local variable instead, (or the special Result
variable, if your compiler provides that), eg:
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;
这篇关于不重载运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!