C++ 使用智能指针实现链表 - 'head'未在此范围内声明
C++ Linked List implementation using smart pointers - 'head' was not declared in this scope
我在理解为什么我的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
。
相关文章:
- .cpp和.h文件中的模板专用化声明
- 未在作用域中声明unordered_map
- C++避免重复声明的语法是什么
- 如何确保C++函数在定义之前声明(如override关键字)
- 错误:未在此范围内声明'reverse'
- 奇怪的(对我来说)返回声明 - 在谷歌上找不到任何关于它的信息
- 为什么在定义函数之前先声明它
- 如何声明特征矩阵,然后通过嵌套循环初始化它
- #ifdef和未声明的标识符
- 没有显式声明的int[]中的foreach
- 在基于范围的for循环中使用结构化绑定声明
- 在将变量声明为引用时,堆在释放后使用
- C++:无法访问声明的受保护成员
- 为什么我不能在一个类的不同行中声明和定义成员变量?
- 我不明白为什么我声明一个空的内部结构并将其传递给构造函数
- 使用cmake从源代码构建MySQL连接器/C++失败(与以前的声明冲突)
- 在函数内部的声明中初始化数组,并在外部使用它
- Visual Studio中的函数声明和函数定义问题
- C++ 使用智能指针实现链表 - 'head'未在此范围内声明
- 将 head 设置为 NULL ('NULL' : 未声明的标识符)