如何从另一个C ++类访问结构?

时间:2015-08-12 11:41:35

标签: c++ qt struct forward-declaration

你好我在TreeItem类中有一个结构:

// TreeItem.h
class TreeItem
{
public:

  struct TreePair
  {
    QString   sa_key;
    QVariant  sa_value;
  };

  //... blabla
}

我想从另一个类TreeModel访问该结构TreePair,该类已经在其标题中包含了类TreeItem:

// TreeModel.h
class TreeItem;
class TreeModel : public QAbstractItemModel
{
    Q_OBJECT
    //..
}

// TreeModel.cpp
TreeModel::TreeModel(const QStringList &headers, const QString &data, QObject *parent)
  : QAbstractItemModel(parent)
{
  QVector<TreePair> rootData; // TreePair was not declared in this scope
}

我的strcut没有在这个范围内声明?在每个课程中,它已经自动设置为

#ifndef _TREEITEM_H
#define _TREEITEM_H
#endif

1 个答案:

答案 0 :(得分:1)

由于TreePair嵌套在TreeItem内,因此需要

QVector<TreeItem::TreePair> rootData;