Using C++ Builder in Rad Studio 10.4
Why can't I create a pointer to a TTreeNode
?
I have a TTreeView
called BookmarksTree
, and I want to loop through all of its nodes. When I try and compile this:
TTreeNode *Node;
Node = BookmarksTree->Items[1];
I get a compiler error:
assigning to 'Vcl::Comctrls::TTreeNode *' from incompatible type 'Vcl::Comctrls::TTreeNodes'
According to Vcl.ComCtrls.TCustomTreeView.Items, I should be able to use
MyTreeNode = TreeView1->Items[[1]];
Has anyone any idea what's wrong here?
BookmarksTree->Items
is a pointer to a singleTTreeNodes
object. You are trying to perform pointer arithmetic to access a node as if theItems
were an array ofTTreeNode*
pointers, which is simply not the case.You need to use the
TTreeNodes::Item[]
sub-property instead, eg:Alternatively, you can use the
TTreeNodes::operator[]
, but that requires you to dereference theTTreeNodes*
pointer first, eg:Alternatively, in the Clang-based compilers only, you can use C++ iterators, per C++ Iterator Support for Delphi Enumerable Types and Containers, eg:
Or a range-for loop (which uses iterators internally):