c++ - 第一次尝试创建链表

标签 c++ linked-list

首先我想说对不起,我敢肯定这个问题很简单, 但这是我第一次尝试创建链表。

问题是当我尝试分配内存并在“nodeHead”中放置一个新节点地址时,我收到一条错误消息:

error C2440: '=' : cannot convert from 'MatchmakingAgency::Node *' to 'Node *'

IntelliSense: a value of type "MatchmakingAgency::Node *" cannot be assigned to an entity of type "Node *"

文件如下:

MatchmakingAgency.h - The database class

class Node;

#include "Client.h"
#ifndef _MatchmakingAgency_H_
#define _MatchmakingAgency_H_

class MatchmakingAgency
{
private:
    Node* nodeHead;
    Node* nodeTail;

    //Node class
    class Node
    {
    private:
        Client* client;
        Node* next;
    public:
        Node();
        Node(Client*);
        Node(Client*,Node*);
    };
    //end of Node class

public:
    MatchmakingAgency();
    MatchmakingAgency(Client*);
    ~MatchmakingAgency();

    void printDatabase();

};

#endif

MatchmakingAgency.cpp

#include "MatchmakingAgency.h"

MatchmakingAgency::MatchmakingAgency(){
    nodeHead = nodeTail = NULL;
}

MatchmakingAgency::MatchmakingAgency(Client* data){
    nodeHead = new Node(data);
}

MatchmakingAgency::~MatchmakingAgency(){
}

Node.cpp

#include "MatchmakingAgency.h"

MatchmakingAgency::Node::Node(){
    next = NULL;
}

MatchmakingAgency::Node::Node(Client* data){
    client = data;
    next = NULL;
}

MatchmakingAgency::Node::Node(Client* data,Node* nextnode){
    client = data;
    next = nextnode;
}

希望你能帮助我, 请不要生气 ;)

非常感谢!

最佳答案

移动前向声明:

class Node;

从顶部进入 MatchmakingAgency.h 文件中的 MatchmakingAgency 类。

//class Node; << remove this

#include "Client.h"
#ifndef _MatchmakingAgency_H_
#define _MatchmakingAgency_H_

class MatchmakingAgency
{
private:
    class Node; // << move here
    Node* nodeHead;

关于c++ - 第一次尝试创建链表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25465040/

相关文章:

c++ - 64位指针的无锁内存回收

c++ - 带链表的回文函数

c++ - 递归在链表c++中添加结构

java - 在用户定义的 LinkedList 中创建 push 和 pop 方法

c++ - 哪个才是真正的冒泡排序,哪个更好?

c++ - Boost.Asio 错误代码为 "An existing connection was forcibly closed by the remote host"

javascript - 使用递归函数查找链表内部对象的 value 属性

c - 链接列表 : Dequeue correctly returns popped data, 但尝试使用打印时出现段错误

c++ - 跨平台 C++...一个文件中特定于平台的代码部分,还是单独的类?

c++ - 在计算中使用空指针