这些返回指针的函数调用有什么区别?

时间:2017-04-18 09:13:19

标签: c++ qt

我正在Qt解析XML文件,使用QTreeWidget显示XML的各个部分。我想保持指向每个部分“标题”的指针。

我已经定义了这些指针:

QTreeWidgetItem* groupExport,
                 groupDesign,
                 groupSheet,
                 groupTitleBlock,
                 groupComponents,
                 groupLibParts,
                 groupLibraries,
                 groupNets;

我有这些功能:

//////////////////////////////////////////////////////////////////////////////
/// treeAddRoot
///---------------------------------------------------------------------------
QTreeWidgetItem* window_Main::treeAddRoot(QString name) {
    QTreeWidgetItem *treeRoot = new QTreeWidgetItem(ui->treeSourceFile);
    ...
    return treeRoot;
}
QTreeWidgetItem* window_Main::treeAddRoot(QString name, QString description) {
    QTreeWidgetItem *treeRoot = new QTreeWidgetItem(ui->treeSourceFile);
    ...
    return treeRoot;
}

//////////////////////////////////////////////////////////////////////////////
/// treeAddGroup
///---------------------------------------------------------------------------
QTreeWidgetItem* window_Main::treeAddGroup(QTreeWidgetItem *treeParent, QString name) {
    QTreeWidgetItem *treeChildGroup = new QTreeWidgetItem(treeParent);
    ...
    return treeChildGroup;
}
QTreeWidgetItem* window_Main::treeAddGroup(QTreeWidgetItem *treeParent, QString name, QString description) {
    QTreeWidgetItem *treeChildGroup = new QTreeWidgetItem(treeParent);
    ...
    return treeChildGroup;
}

//////////////////////////////////////////////////////////////////////////////
/// treeAddChild
///---------------------------------------------------------------------------
void window_Main::treeAddChild(QTreeWidgetItem *treeParent, QString name) {
    QTreeWidgetItem *treeChild = new QTreeWidgetItem(treeParent);
    ...
    treeParent->addChild(treeChild);
}
void window_Main::treeAddChild(QTreeWidgetItem *treeParent, QString name, QString description) {
    QTreeWidgetItem *treeChild = new QTreeWidgetItem(treeParent);
    ...
}

这些是为解析时“临时”使用而定义的:

QString tempTag, tempValue;

对于解析代码,编译器似乎认为这没问题:

if (tempTag.contains("export")) {
   ...            
   groupExport = treeAddRoot(tempTag);
}

然而,几行后,用这个:

if (tempTag.contains("design")) {
    ...
    groupDesign = treeAddGroup(groupExport, tempTag);
}

编译器错误:

error: no match for ‘operator=’ (operand types are ‘QTreeWidgetItem’ and ‘QTreeWidgetItem*’)
groupDesign = treeAddGroup(groupExport, tempTag);
            ^

是否有人可以帮助我理解差异,以及为什么编译可以正常而另一个失败?

提前致谢。

1 个答案:

答案 0 :(得分:1)

定义像这样的指针:

QTreeWidgetItem *groupExport,
                *groupDesign,
                *groupSheet,
                *groupTitleBlock,
                *groupComponents,
                *groupLibParts,
                *groupLibraries,
                *groupNets;