C++ 使用智能指针实现链表 - 'head'未在此范围内声明

C++ Linked List implementation using smart pointers - 'head' was not declared in this scope

本文关键字:head 声明 范围内 实现 指针 链表 C++ 智能      更新时间:2023-10-16

我在理解为什么我的head节点没有被声明为在我的Deque.cpp函数范围内可用时遇到了问题。

我目前只写了print_queue()函数。

我认为这是因为我的头没有初始化为任何东西-但是当我试图创建一个Deque::Deque()构造函数来初始化时,我得到了一个编译错误,说它是显式默认的,我不能为它创建一个构造函数。

解决这个问题的最好方法是什么?

Deque.cpp

#include "Deque.h"
using std::cout;
using std::endl;
void Deque::insert_front(int)
{
}
void Deque::insert_back(int)
{
}
int Deque::remove_front()
{
    return 0;
}
int Deque::remove_back()
{
    return 0;
}
int Deque::peek_front() const
{
    return 0;
}
int Deque::peek_back() const
{
    return 0;
}
bool Deque::empty() const
{
    return 0;
}
int Deque::size() const
{
    return 0;
}
void print_queue(std::string& label)
{
    Node* p = head;
    cout << "This is the linked list: " << endl;
    while ( p != NULL)
        {
            cout << head << endl;
        }
}

Deque.h

#include "Node.cpp"
#include <memory>

class Deque{
public:
    Deque() = default;
    Deque(const Deque&);
    ~Deque(); //must use constant space
    Deque& operator=(const Deque&); //we can use assignment in this assignement lols.

    void insert_front(int); //Must run in O(1) time
    void insert_back(int);
    int remove_front(); // O(1) - if the deque is empty - throw a runtime_error
    // (this error class is defined in the stdexcept library file)
    int remove_back();
    int peek_front() const;  //throw run_time if empty, return value dont remove
    int peek_back() const;
    bool empty() const;
    int size() const; //O(1) - return number of stored items in deque
    int size_LL = 0;
    void print_queue(const std::string& label) const; //prints all nodes in queue,
    //together with pointers to head and tail and also size of queue.
    //routine calls the node output function - not tested
    //helper methods - deep copy, used by copy and operator=
    Deque deep_copy(const Deque&);
private:
    std::unique_ptr<Node> head;
    std::unique_ptr<Node> tail;
    friend Node;
};

Node.cpp

#include "Node.h"
std::ostream& operator<<(std::ostream& out, const Node& n) {
    return out << &n << ": " << n.val << " -> " << n.next.get();
}

Node.h

#ifndef NODE_H
#define NODE_H
#include <iostream>
#include <memory>
class Node {
public:
    Node(const Node& n) : val{n.val}, next{}
    {
    }
    Node(int v, std::unique_ptr<Node> n) : val{v}, next{move(n)}
    {
    }
    Node(int v) : val{v}
    {
    }
private:
    int val = 0;
    std::unique_ptr<Node> next = nullptr;
    friend class Deque;
    friend std::ostream& operator<<(std::ostream&, const Node&);
};
#endif

main.cpp

#include <iostream>
#include "Deque.cpp"
using std::cout;
using std::endl;
int main()
{
    Deque dq1;
    cout << dq1.empty() << " - 1" << endl;
    dq1.insert_front(42);
    dq1.insert_back(216);
    cout << dq1.peek_front() << " - 42" << endl;
    cout << dq1.peek_back() << " - 216" << endl;
    cout << dq1.size() << " - 2" << endl;
    dq1.print_queue("dq1 before copy constructor and copy assignment");
    Deque dq2(dq1);
    dq2.print_queue("dq2 after copy constructor");
    Deque dq3;
    dq3 = dq1;
    dq3.print_queue("dq3 after copy assignment");
    cout << dq1.remove_front() << " - 42" << endl;
    cout << dq1.remove_back() << " - 216" << endl;
    dq1.print_queue("dq1 should be empty");
    cout << dq2.peek_front() << " - 42" << endl;
    cout << dq2.peek_back() << " - 216" << endl;
    dq3.print_queue("After two removes from dq1");
    cout << dq3.peek_front() << " - 42" << endl;
    cout << dq3.peek_back() << " - 216" << endl;
    return 0;
}
#include "Node.cpp"

永远不要包含源文件。如果在Deque.h中这样做,那么最多只能有一个源文件包含Deque.h。这大大降低了它的可重用性。

'head'未在此范围内声明

head实际上没有在自由函数void print_queue(std::string& label)中声明。也许您打算定义成员函数void Deque::print_queue(std::string& label),其中声明了成员变量head