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

时间:2016-10-10 04:13:43

标签: c++ c++11 compiler-errors linked-list smart-pointers

我在理解为什么我的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;
}

1 个答案:

答案 0 :(得分:2)

#include "Node.cpp"

不要包含源文件。如果您在Deque.h中执行此操作,则最多一个源文件可以包含Deque.h。这大大降低了它的可重用性。

  

'head'未在此范围内声明

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