簡體   English   中英

使用智能指針的C ++鏈表實現-在此范圍內未聲明“ head”

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

我在理解為什么在我的Deque.cpp函數范圍內為什么未聲明head節點可用時遇到了問題。

我目前只寫了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;
        }

}

德克

#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();
}

節點

#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 ,則最多只能有一個源文件包含Deque.h 這大大降低了其可重用性。

未在此范圍內聲明“頭”

確實沒有在自由函數void print_queue(std::string& label)聲明head 也許您打算定義成員函數void Deque::print_queue(std::string& label) ,在該函數中聲明成員變量head

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM