如何修复程序循环引用?
我是 Delphi 编程领域的新手,在控制台应用程序中的过程中调用过程时遇到问题。
我的简单应用程序是通过 Windows 上的 telnet 服务器运行的项目库存。 我使用旧的 thinkpad 作为运行 Linux 的瘦客户端和 telnet 客户端。
使用 Delphi XE,我遇到了先有鸡还是先有蛋的情况。
我得到 addscreen 未声明的标识符...它已声明但在主屏幕下! 如果我将 addscreen 过程放在主屏幕上,则 addscreen 过程中对主屏幕的任何调用都会导致错误未声明的标识符主屏幕!
简单来说,如何让程序在程序中处处被调用呢?
我已经尝试过接口和实现,但它在控制台应用程序中无效!
program Project1;
{$APPTYPE CONSOLE}
uses
SysUtils, windows, messages, Console in 'Console.pas';
procedure mainscreen;
var
choice: string;
begin
clrscr;
writeln(' ------------------------------------------------------------------------------------------------------------------------------');
writeln(' | Inventory Management 0.1 ALPHA |');
writeln(' ------------------------------------------------------------------------------------------------------------------------------');
writeln('');
writeln('');
writeln('');
writeln('');
writeln('');
writeln(' Make a choice: Add(a), Remove(r), Edit(e), Backup Database(bd), Mass Add(ma), Mass Remove(mr)');
writeln('?:');
readln(choice);
if choice = 'a' then
addscreen
else
mainscreen;
end;
procedure addscreen;
var
choice: string;
begin
clrscr;
writeln(' ------------------------------------------------------------------------------------------------------------------------------');
writeln(' | Add an Item |');
writeln(' ------------------------------------------------------------------------------------------------------------------------------');
writeln('');
writeln('');
writeln('');
writeln(' Not yet ready!');
writeln(' Press any key to return to the main menu...');
readln(choice);
mainscreen;
end;
begin
mainscreen;
textbackground(black);
textcolor(lightgray);
ExitProcess(0);
end.
非常感谢!
I'm new in the Delphi programming scene and i have trouble calling a procedure in a procedure in my console application.
My simple application is for a item inventory running through a telnet server on windows.
I'm using a old thinkpad as my thinclient running linux and a telnet client.
Using Delphi XE I've ran into a chicken or the egg situation.
I get addscreen undeclared indentifier... it is declared but under mainscreen !!!
If i put addscreen procedure over the mainscreen one, any call to mainscreen in the addscreen procedure make me an error undeclared indentifier mainscreen!
In simple terms, how to make the procedure to be call everywhere in the program?
I've tried interface and implementation but its not valid in a console application program!
program Project1;
{$APPTYPE CONSOLE}
uses
SysUtils, windows, messages, Console in 'Console.pas';
procedure mainscreen;
var
choice: string;
begin
clrscr;
writeln(' ------------------------------------------------------------------------------------------------------------------------------');
writeln(' | Inventory Management 0.1 ALPHA |');
writeln(' ------------------------------------------------------------------------------------------------------------------------------');
writeln('');
writeln('');
writeln('');
writeln('');
writeln('');
writeln(' Make a choice: Add(a), Remove(r), Edit(e), Backup Database(bd), Mass Add(ma), Mass Remove(mr)');
writeln('?:');
readln(choice);
if choice = 'a' then
addscreen
else
mainscreen;
end;
procedure addscreen;
var
choice: string;
begin
clrscr;
writeln(' ------------------------------------------------------------------------------------------------------------------------------');
writeln(' | Add an Item |');
writeln(' ------------------------------------------------------------------------------------------------------------------------------');
writeln('');
writeln('');
writeln('');
writeln(' Not yet ready!');
writeln(' Press any key to return to the main menu...');
readln(choice);
mainscreen;
end;
begin
mainscreen;
textbackground(black);
textcolor(lightgray);
ExitProcess(0);
end.
Thank you very much!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
编译器从上到下遍历文件。当它需要处理对
addscreen
的调用时,您尚未声明addscreen
。由于您有一个循环引用(
addscreen
调用mainscreen
,反之亦然),您需要使用所谓的前向引用来打破它。在mainscreen
之前添加这一行,因此代码的组织应该如下所示:
如果您没有循环引用,那么您可以简单地重新排序过程,以便
addscreen
在主屏幕
之前声明。The compiler passes through the file from top to bottom. At the point where it needs to deal with the call to
addscreen
, you have not yet declaredaddscreen
.Since you have a circular reference (
addscreen
callsmainscreen
and vice versa) you need to break that with what is known as a forward reference. Add this line beforemainscreen
So the organisation of the code should look like this:
If you did not have a circular reference then you could simply reorder the procedures so that the
addscreen
was declared beforemainscreen
.