如何修复程序循环引用?

发布于 2024-12-10 02:00:08 字数 2023 浏览 0 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

橘和柠 2024-12-17 02:00:08

编译器从上到下遍历文件。当它需要处理对 addscreen 的调用时,您尚未声明 addscreen

由于您有一个循环引用(addscreen 调用 mainscreen,反之亦然),您需要使用所谓的前向引用来打破它。在 mainscreen 之前添加这一行,

procedure addscreen; forward;

因此代码的组织应该如下所示:

procedure addscreen; forward;

procedure mainscreen;
begin
  ...
end;

procedure addscreen;
begin
  ...
end;

如果您没有循环引用,那么您可以简单地重新排序过程,以便 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 declared addscreen.

Since you have a circular reference (addscreen calls mainscreen and vice versa) you need to break that with what is known as a forward reference. Add this line before mainscreen

procedure addscreen; forward;

So the organisation of the code should look like this:

procedure addscreen; forward;

procedure mainscreen;
begin
  ...
end;

procedure addscreen;
begin
  ...
end;

If you did not have a circular reference then you could simply reorder the procedures so that the addscreen was declared before mainscreen.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文