小编Sud*_*ngh的帖子

尝试引用已删除函数的唯一指针

您好,我正在尝试使用指针并学习C++. 下面是我的代码,我已经注释了 main 函数中的代码行。调试问题但是,我无法这样做。我缺少什么?我的move()方法不对吗insertNode()?我得到的错误位于代码下方:

#include<memory>
#include<iostream>

struct node{
    int data;
    std::unique_ptr<node> next;
};


void print(std::unique_ptr<node>head){
        while (head)
            std::cout << head->data<<std::endl;
    }


std::unique_ptr<node> insertNode(std::unique_ptr<node>head, int value){
        node newNode;
        newNode.data = value;
        //head is empty
        if (!head){
            return std::make_unique<node>(newNode);
        }
        else{
            //head points to an existing list
            newNode.next = move(head->next);
            return std::make_unique<node>(newNode);
        }
    }



auto main() -> int
{
    //std::unique_ptr<node>head;
    //for (int i = 1; i < 10; i++){
    //  //head = insertNode(head, i);
    //}
} …
Run Code Online (Sandbox Code Playgroud)

c++ pointers unique-ptr

1
推荐指数
1
解决办法
1万
查看次数

标签 统计

c++ ×1

pointers ×1

unique-ptr ×1