我想编写此代码来执行与我的copyItemFromTree
方法相关的操作,但在我的函数结束时,我有运行时检查失败#2 ...错误,你能帮我吗?解决这个问题?
pFrom
和pTo
是Ctreecntrl
mfc对象;
TVITEM ItemFrom, ItemTo;
TCHAR szTextFrom[1024];
ItemFrom.pszText = szTextFrom;
ItemFrom.cchTextMax = 1024;
ItemFrom.hItem = hItem;
TCHAR szTextTo[1024];
ItemTo.pszText = szTextTo;
ItemTo.cchTextMax = 1024;
ItemTo.hItem = hItemInTo;
if (pFrom->GetItem(&ItemFrom) && pTo->GetItem(&ItemTo))//I think the problem is here but i don't know how to solve this.
{
if (ItemFrom.cChildren)
{
ItemTo.mask = TVIF_CHILDREN;
ItemTo.cChildren = ItemFrom.cChildren;
pTo->SetItem(&ItemTo);
}
}
答案 0 :(得分:0)
很可能“运行时检查失败#2”是因为项目未初始化为零。掩码也没有设置。这会导致未定义的行为。
尝试以下操作并分隔线条以便于调试:
TVITEM ItemFrom = { 0 };
TVITEM ItemTo = { 0 };
TCHAR szTextFrom[1024];
ItemFrom.pszText = szTextFrom;
ItemFrom.cchTextMax = 1024;
ItemFrom.hItem = hItem;
ItemFrom.mask = TVIF_TEXT;
TCHAR szTextTo[1024];
ItemTo.pszText = szTextTo;
ItemTo.cchTextMax = 1024;
ItemTo.hItem = hItemInTo;
ItemTo.mask = TVIF_TEXT;
ASSERT(pFrom->GetItem(&ItemFrom));
ASSERT(pTo->GetItem(&ItemTo));
if (pFrom->GetItem(&ItemFrom) && pTo->GetItem(&ItemTo))
{
...
}
此行不会复制孩子:
ItemTo.mask = TVIF_CHILDREN ;
ItemTo.cChildren = ItemFrom.cChildren;
pTo->SetItem(&ItemTo);
您必须将孩子从一棵树逐个复制到另一棵树。您可以尝试以下内容:
if (hItemFrom)
pTo->InsertItem(pFrom->GetItemText(hItemFrom), hParent);