我正在用pascal写一个程序。但不知何故得到错误的原因我不明白。你能帮忙吗?
Program main;
Procedure A(n : longint);
Procedure B(n : longint);
Procedure E(n : longint);
Procedure D(n : longint);
begin
WriteLn ('In D. No Call...');
end;
begin
WriteLn ('In E ');
D(n);
end;
begin
WriteLn ('In B ');
WriteLn ('Calling A Do -1 ',n);
if n = 1
then
A(1);
end;
begin
WriteLn ('In A ');
B(n);
WriteLn ('Calling B ',n);
if(n<1)
then
begin
C(n);
end;
end;
begin
A(1);
end.
我试图从主程序调用proc A,然后A调用B,依此类推。我在C:
中得到了令人卷扰的错误Here are the errors I get:
Free Pascal Compiler version 2.2.0 [2009/11/16] for i386
Copyright (c) 1993-2007 by Florian Klaempfl
Target OS: Linux for i386
Compiling prog.pas
prog.pas(32,14) Error: Identifier not found "C"
prog.pas(32,17) Error: Illegal expression
prog.pas(37,4) Fatal: There were 2 errors compiling module, stopping
Fatal: Compilation aborted
Error: /usr/bin/ppc386 returned an error exitcode (normal if you did not specify a source file to be compiled)
答案 0 :(得分:2)
只是为了补充SteB的答案,这里有一些可能存在的错误。
C
E
重命名为C
,您仍会收到编译错误,因为A
无法调用C
,因为它不在范围内,因为嵌套程序的范围(在B
的范围内)。请参阅Static scoping in nested procedures A(1)
调用B(1)
调用A(1)
... e.g。如果将E
重命名为C
并缩进:
Program main;
Procedure A(n : longint);
Procedure B(n : longint);
Procedure C(n : longint);
Procedure D(n : longint);
begin
WriteLn ('In D. No Call...');
end;
begin
WriteLn ('In C ');
D(n);
end;
begin
WriteLn ('In B ');
WriteLn ('Calling A Do -1 ',n);
if n = 1
then A(1); (* Compiles OK, B is nested within A, but watch recursion *)
end;
begin
WriteLn ('In A ');
B(n);
WriteLn ('Calling B ',n);
if(n < 1)
then
begin
C(n); (* Doesnt compile, since C is nested within B and not in scope of A *)
end;
end;
begin
(* You are in Main here *)
A(1);
end.
答案 1 :(得分:1)
我认为你错过了“程序C” 我没有看到任何地方定义的“过程C”,错误表明程序也看不到这个例程。
一些更好的缩进可以使事情更加清晰,你使用的是嵌套程序(不是最佳实践),但程序A和B具有相同的缩进程度。