c++builder - 无法从“TFmxChildrenList”转换为“TRectangle *”
问题描述
我是使用 C++ Builder 的新手,如果我犯了任何基本错误,我深表歉意。
我绘制了一个名为“Collection”的 TLayout,其中包含一个 5x5 的 TRectangle 网格。细胞被命名为“CellXY”。我认为在表单上绘制这些内容可能比用代码实例化它们更容易,现在我不这么想,但我仍然想以这种方式解决问题以更好地理解。
我正在尝试编写一个方法,该方法将返回一个指向 TRectangle 的指针,其名称包含传递给该方法的坐标。
目前,我正试图通过迭代 TLayout 集合的孩子来做到这一点:
TRectangle* __fastcall TForm2::getCellRectangleFromCoordinate(int X, int Y){
TRectangle* targetCell = NULL;
char targetCellName[6];
sprintf(targetCellName, "Cell%i%i", X, Y);
for (int cIndex = 0; cIndex < Collection->ChildrenCount; ++cIndex)
{
TRectangle* cellRect = (TRectangle*) Collection->Children[cIndex]; // Error Here
string cellName = cellRect->Name;
if (targetCellName == cellName) {
targetCell = cellRect;
break;
}
}
return targetCell;
}
但我得到一个错误阅读:
E2031 Cannot cast from 'TFmxChildrenList' to 'TRectangle *'
如果有人可以提供帮助,我将不胜感激!
解决方案
该Children
属性是一个指向TFmxChildrenList
内部保存对象数组的类类型 ( ) 的指针。 Children
不是实际的数组本身,就像您试图将其视为一样。
Children[cIndex]
正在使用指针算术,这不是您在这种情况下想要的。您需要Children->Items[]
通过更改以下语句来使用子属性:
Collection->Children[cIndex]
对此:
Collection->Children->Items[cIndex]
或这个:
(*(Collection->Children))[cIndex]
推荐阅读
- hyperledger-fabric - Hyperledger Fabric 警告。此排序器正在兼容模式下运行
- java - JSON 路径 2.3.0 与 hadoop 2.7 环境 JSON-smart1.2.0.jar 冲突
- javascript - 使用 ajax 发布数据,然后使用发布的数据转到该页面?
- html - Ionic 4 - 来自数据列表的离子输入自动完成
- java - 这种成对类的情况是否有设计模式?
- javascript - 为什么 j < i 在此解决方案中用于将矩阵旋转 90 度?
- nuget - 如何在 Azure Webapp (api) 中安装和实现 HtmlRenderer.PdfSharp
- checksum - md5sum - 忽略尾随空格
- amazon-web-services - 使用 Lambda 授权方的 AWS Cognito 和 API 网关
- json - Swift 4 JSON 可解码多维和多类型数组读取涌入数据