unique_ptr , how to create and use

Thanks JL but stillI am stuck in this Insert function at the marked line.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45


Node* LinkedList::insert(Node* pos, const std::string& value) {
        // pos point to node where to insert before
	// this line failes in run
	std::unique_ptr<Node> pNode = std::make_unique<Node>(value, std::move(pos->getPrev()->next), pos->getPrev());

	pos->prev = pNode.get();

	pos->getPrev()->next = std::move(pNode);

	return pNode.get();

		
class Node {
public:
    const std::string value;    // The data held by the LinkedList
    std::unique_ptr<Node> next; // unique_ptr to the next node
    Node* prev;                 // raw (non-owning) ptr to the previous node
public:
    Node() : value(), next(nullptr), prev(nullptr) {}
    // construct a node with string value, a unique_ptr to the next node, and a pointer to the previous node
    Node(const std::string& value, std::unique_ptr<Node> next, Node* prev)
        : value(value)
        , next(std::move(next))
        , prev(prev)
    {}
    // We can use the default constructor, since unique_ptr takes care of deleting memory
    ~Node() = default;
    // return the value of the node 222
    std::string getValue() const { return value; }

    // return a raw (non-owning) pointer to the next node
    Node* getNext() const { return next.get(); }
    // return a raw (non-owning) pointer to the previous node
    Node* getPrev() const { return prev; }

    // write the value of the node to the ostream
    friend std::ostream & operator<<(std::ostream & os, const Node & node);

    friend class LinkedList;
};


Last edited on
Something along these lines, perhaps:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <memory>
#include <string>

struct str_list {

    bool empty() const { return first == nullptr ; }

    void push_front( std::string str ) {

        auto p = std::make_unique<node>( std::move(str) ) ;

        if( !empty() )
        {
            first->prev = p.get() ;
            p->next = std::move(first) ;
        }

        first = std::move(p) ;
    }

    struct node {

        explicit node( std::string str ) : value( std::move(str) ) {}
        std::string value ;
        std::unique_ptr<node> next ;
        node* prev = nullptr ;
    };

    std::unique_ptr<node> first ;
};

int main() {

    str_list lst ;

    for( std::string str : { "abc", "def", "ghi", "jkl", "mno", "pqr" } )
        lst.push_front(str) ;

    for( auto n = lst.first.get() ; n != nullptr ; n = n->next.get() )
        std::cout << n->value << '\n' ;
}

http://coliru.stacked-crooked.com/a/221d5b42e5274988
Thanks JL I am a lot further but still absolutely stuck.
I have updated the code in the first post.
Please don't modify your previous messages, just make a new one. It makes it much harder for others to follow the thread.
of course , sorry
olefredrik wrote:
Node(const std::string& value, std::unique_ptr<Node> next, Node* prev)

Are you sure you can copy a std::unique_ptr?
Are you sure you can copy a std::unique_ptr?

No copy is implied if the argument is an rvalue. e.g., one could pass std::move(my_unique_ptr).
Last edited on
pos->getPrev()->next = std::move(pNode);

return pNode.get();

Think this also is invalid because after move of pNode its gone and the reference in the return pNode.get() is invalid ?
Solved:

Node* LinkedList::insert(Node* pos, const std::string& value) {
Node * pNode; // non-owner pointer to new node
if (pos == begin()) { // insert first in list or when list is empty
std::unique_ptr<Node> upNode = std::make_unique<Node>(value,std::move(head), nullptr);
pNode = upNode.get();
head = std::move(upNode);
pos->prev = pNode;
}else {
std::unique_ptr<Node> upNode = std::make_unique<Node>(value,std::move(pos->prev->next), pos->prev);
pNode = upNode.get();
pos->prev->next = std::move(upNode);
pos->prev = pNode;
}
return pNode; // return new node
}



Last edited on
Topic archived. No new replies allowed.