访问项目 cpp 文件中自动创建的 DataModule 时,Builder C++ (XE7) 编译器错误 2315

Builder C++ (XE7) compiler Error 2315 when accessing an auto created DataModule in Project cpp file

我正在将用 Delphi 7 编写的旧项目转换为 C++Builder (XE7)。

在 Delphi 7 项目文件中,我可以通过引用轻松访问自动创建的表单和数据模块。但是在 C++Builder 中,当我做同样的事情时,我得到这个错误:

E2315 'con1' is not a member of 'TDM1', because the type is not yet defined.

这是 c++ 项目文件的一部分:

Application->Initialize();
Application->MainFormOnTaskBar = true;
Application->CreateForm(__classid(TfrmMain), &frmMain);
Application->CreateForm(__classid(TDM1), &DM1);
DM1->con1->Open();//ERROR 2315
Application->Run();

解决此问题的解决方法是什么?

对于您的尝试,您需要在主项目文件中为 TDM1 的头文件添加一个 #include 语句。在C++Builder中,自动创建的Form/DataModule默认只是在主工程文件中前向声明,只是为了满足CreateForm()的引用。但是,为了访问任何成员,您需要访问完整的 class 声明。

话虽如此,鉴于您显示的代码,对 con1->Open() 的调用应该移至 TDM1 的构造函数,因此当 CreateForm(&DM1) 是叫。那么你不需要知道主工程文件中TDM1的完整类型