我想在我的打印功能中創建一個臨時變量'p'。 我收到一個編譯器錯誤和2個警告,我不知道該如何處理。C++ - 鏈接列表 - 無法創建一個臨時節點
大多數功能在稍後有一個處理打印功能的時候纔會被填寫。
我也重新定義了cout < < & n打印出鏈接列表的值。
unique_ptr.h||In instantiation of 'typename std::_MakeUniq<_Tp>::__single_object std::make_unique(_Args&& ...) [with _Tp = Node; _Args = {const std::unique_ptr<Node, std::default_delete<Node> >*}; typename std::_MakeUniq<_Tp>::__single_object = std::unique_ptr<Node>]':|
Deque.cpp|46|required from here|
/usr/include/c++/6/bits/unique_ptr.h|787|error: invalid conversion from 'const std::unique_ptr<Node>*' to 'int' [-fpermissive]|
Node.h|15|note: initializing argument 1 of 'Node::Node(int)'|
|| ===建立失敗:1個誤差(S),2警告(S)(0分鐘0秒)=== |
Main.cpp的
#include <iostream>
#include "Node.h"
#include "Deque.cpp"
using std::cout;
using std::endl;
std::ostream& operator<<(std::ostream& out, const Node& n) {
return out << &n << ": " << n.val << " -> " << n.next.get();
}
int main()
{
return 0;
}
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
Deque.h
#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 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
private:
std::unique_ptr<Node> head;
std::unique_ptr<Node> tail;
friend Node;
};
Deque.cpp
#include "Deque.h"
#include <memory>
using std::cout;
using std::endl;
void Deque::print_queue(const std::string& label) const
{
std::unique_ptr<Node> p = std::make_unique<Node>(&head);
cout<< "This is the linked list: " << endl;
while (p != NULL)
{
cout<< &head;
}
}
我也試着'的std ::的unique_ptr P =頭;'這導致一個不同的錯誤'使用刪除功能的std「::的unique_ptr <_Tp, _Dp> ::的unique_ptr(常量的std ::的unique_ptr <_Tp, _Dp>&)'雖然我覺得這種方式比前者更合適。 –
TigerCode
代碼太多了。請將其縮小到最小的,自包含的,可編譯的示例,它仍然會重現錯誤。 – GManNickG
會做,謝謝。 - 行爲已經完成。 – TigerCode