我正在努力创建一个指向自制类对象的指针的 QList,在本例中为 Node。我尝试使用与 Library An Introduction to Design Patterns in C++ with Qt 示例中相同的方法,Ezust 和 Ezust。
由于某种原因,容器类 NodeList 无法访问 Node 类中的构造函数并提示它是私有(private)的,即使它是公共(public)的。
我曾尝试在 Node 的定义中使用 friend 关键字,但这也不起作用。我看不出问题出在哪里,因为我引用指针的方式与教科书中的工作示例完全相同。
#ifndef NODE_H
#define NODE_H
#include <QString>
#include <QList>
class Node
{
public:
Node() {}
void setNodeLabel(QString label);
QString getNodeLabel();
Node(QString label);
private:
QString nodeLabel;
};
class NodeList : public QList<Node*>
{
public:
NodeList() {}
~NodeList();
void addNode(Node*& node);
private:
NodeList(const NodeList&);
NodeList& operator=(const NodeList&);
};
#endif // NODE_H
我在尝试编译时遇到的错误如下:
\qlist.h:106: error: 'struct QList<Node*>::Node' is private
struct Node { void *v;
^
\node.h:25: error: within this context
void addNode(Node*& node);
^
最佳答案
NodeList
源自 QList<Node*>
,它有一个名为 Node
的嵌套类,它在 private
中节QList
.当您使用非限定类型时 Node
NodeList
中的任意位置,该名称解析为 QList<Node*>::Node
.因此,您会收到该错误。
除非您打算向 NodeList
添加更多功能,你可以使用:
using NodeList = QList<Node*>;
并摆脱类 NodeList
.
关于c++ - 我的类(指向另一个类的指针的容器)似乎无法访问公共(public)构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38161366/