Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Circular reference between class

i need develop the next diagram class: enter image description here
I wrote code, but, have problems of circular unit reference.

The XmlFileManager Class contains:

unit XmlFileManager;
interface
uses
  xmldom, XMLIntf, msxmldom, XMLDoc, SysUtils, DateUtils, Classes, Dialogs,
  XmlEnpManager;
type
  TXmlFileManager = class
  private
    [...]
    xmEnp: TXmlEnpManager;
    xmEnpInicial: TXmlEnpManager;
    xmEnpFinal: TXmlEnpManager;
[...]
end.

The abstract class, XmlNodeManager:

unit XmlNodeManager;
interface
uses
  xmldom, XMLIntf, msxmldom, XMLDoc, SysUtils, DateUtils, Classes, Dialogs,
  XmlFileManager;
type
  TXmlNodeManager = class
   protected
        { sgy alias para strategy }
        sgyIterator: Integer;
        sgyContext: TXmlFileManager;
        sgyAttributes: TStringList;
        sgyNode: IXMLNode;
[...]
end.

And the XmlEnpManager concrete class:

unit XmlEnpManager;
interface
uses
  xmldom, XMLIntf, msxmldom, XMLDoc, SysUtils, DateUtils, Classes, Dialogs,
  XmlNodeManager;
type
  TXmlEnpManager = class (TXmlNodeManager)
    public
        constructor Create(aContext: TXmlFileManager); overload; override;
        constructor CreateInicial(aContext: TXmlFileManager); reintroduce; overload;
        constructor CreateFinal(aContext: TXmlFileManager); reintroduce; overload;
[...]
end.

The builds fails with error:

[dcc32 Fatal Error] XmlNodeManager.pas(7): F2047 Circular unit reference to 'XmlFileManager'

Any ideas how to solve this problem ?.

like image 950
ramiromd Avatar asked Mar 22 '23 18:03

ramiromd


1 Answers

Put TXmlFileManager and TXmlNodeManager in both the same unit and the same type section, then make sure that type section starts with this class forward: TXmlNodeManager = class;

See the official documentation: Forward Declarations and Mutually Dependent Classes.

unit XmlFileManagerAndXmlNodeManager;
interface
uses
  xmldom, XMLIntf, msxmldom, XMLDoc, SysUtils, DateUtils, Classes, Dialogs,
[...]

type
  TXmlNodeManager = class;

  TXmlFileManager = class
  private
    [...]
    xmEnp: TXmlEnpManager;
    xmEnpInicial: TXmlEnpManager;
    xmEnpFinal: TXmlEnpManager;
[...]

  TXmlNodeManager = class
   protected
        sgyIterator: Integer;
        sgyContext: TXmlFileManager;
        sgyAttributes: TStringList;
        sgyNode: IXMLNode;
[...]
end.
like image 191
Jeroen Wiert Pluimers Avatar answered Mar 28 '23 17:03

Jeroen Wiert Pluimers