C++ 使用嵌套类的链表?
我有一个类C++ 使用嵌套类的链表?,c++,linked-list,C++,Linked List,我有一个类Agency,它有一个私有的嵌套类节点,应该用来构建客户端对象的链接列表 为了添加一个节点,我需要使用一个重载的+=操作符来接收客户机对象。 当我想要添加第一个对象时:函数调用节点类的集合头成员 但是,一旦我尝试修改head:data的数据成员以指向接收到的客户机对象,并将next修改为NULL时,就会发生运行时错误 我不知道出了什么问题,Client对象按它应该的方式传递(我检查了它)-我认为我在setHead的参数声明中遗漏了一些东西 如果有任何建议,我将不胜感激。 顺便说一句,我
Agency
,它有一个私有的嵌套类节点
,应该用来构建客户端
对象的链接列表
为了添加一个节点,我需要使用一个重载的+=
操作符来接收客户机
对象。
当我想要添加第一个对象时:函数调用节点
类的集合头
成员
但是,一旦我尝试修改head
:data
的数据成员以指向接收到的客户机
对象,并将next
修改为NULL
时,就会发生运行时错误
我不知道出了什么问题,Client
对象按它应该的方式传递(我检查了它)-我认为我在setHead
的参数声明中遗漏了一些东西
如果有任何建议,我将不胜感激。
顺便说一句,我必须使用现有的私有成员,并且setHead
方法必须接收指向Client
的指针
代理机构
class Agency
{
public:
Agency(); //ctor
Agency& operator+=(const Client&); //overloaded += operator
~Agency(); //dtor
private:
class Node //node as nested class
{
public:
Node(); //ctor
void setHead(Client*&); //set head node
private:
Client* data; //points to Client
Node* next; //points to next node on the list
};
Node *head; //points to head node of database
};
Agency.cpp相关方法
void Agency::Node::setHead(Client*& temp)
{
data = temp;
next = NULL;
}
Agency& Agency::operator+=(const Client& client_add)
{
Client* temp = new Client (client_add); //new client object is created using clients copy ctor
if (!head) //if the head node is NULL
{
head->setHead(temp); //assign head node to point to the new client object
}
return *this;
}
编辑:
谢谢你的回答,我还有一个问题:
我想要一个Node
方法,它将返回指向Node
的指针,下面是声明:
`Node* nextNode(Node*);`
功能:
`Node* MatchmakingAgency::Node::nextNode(Node* start)`
导致编译错误:“节点”未命名类型
如何正确声明这种方法?在此代码中:
if (!head) //in the head node is empty
{
head->setHead(temp);
头部
不是“空的”。这是一个空指针。然后取消对空指针的引用,这将导致未定义的行为
也许你本想拥有这个:
head = new Node();
在此代码中,设定头之前
:
if (!head) //in the head node is empty
{
head->setHead(temp);
头部
不是“空的”。这是一个空指针。然后取消对空指针的引用,这将导致未定义的行为
也许你本想拥有这个:
head = new Node();
在此代码中,设定头之前
:
if (!head) //in the head node is empty
{
head->setHead(temp);
头部
不是“空的”。这是一个空指针。然后取消对空指针的引用,这将导致未定义的行为
也许你本想拥有这个:
head = new Node();
在此代码中,设定头之前
:
if (!head) //in the head node is empty
{
head->setHead(temp);
头部
不是“空的”。这是一个空指针。然后取消对空指针的引用,这将导致未定义的行为
也许你本想拥有这个:
head = new Node();
在
setHead
之前,为什么不使用std::list
?不能-这些是赋值规则。为什么不使用std::list
?不能-这些是赋值规则。为什么不使用std::list
?不能-这些是赋值规则。为什么不使用std::list
?不能-这些是赋值规则作业。谢谢,这很有效。我真的很惭愧这就是问题所在。谢谢,这很有效。我真的很惭愧这就是问题所在。谢谢,这很有效。我真的很惭愧这就是问题所在。谢谢,这很有效。事实上,我很惭愧这就是问题所在。