无法将 'Node&' 类型的非常量左值引用绑定到 'const Node' 类型的右值

cannot bind non-const lvalue reference of type 'Node&' to an rvalue of type 'const Node'

我正在尝试使用 struct 实现链表,但我遇到了问题。当我尝试删除节点时出现此错误。有人可以帮我吗?

struct Node {
    int value;
    Node *previous;
    Node *next;
};
// *current_node in all functions is a random address of a node in the linked list 
Node get_node(size_t position, Node *current_node){
    while (current_node->previous){
        current_node = current_node->previous;
    }
    for (int i = 1; i < position; i++){
        current_node = current_node->next;
    }
    return *current_node;
}

void delete_node(Node &node, Node *current_node){
    while (current_node->previous){
        current_node = current_node->previous;
    }
    while (current_node->next){
        if (current_node == &node){
            node.previous->next = node.next;
            node.next->previous = node.previous;
            node.next = nullptr;
            node.previous = nullptr;
        }
        current_node = current_node->suivant;
    }
}

int main(){
    Node node1 = {11, nullptr, nullptr}; // works fine  
    Node node2 = {22, nullptr, nullptr}; // works fine
    add_end(node2, &node1); // works fine
    delete_node(get_node(2, &node1), &node1); // bug is here
}

问题是函数delete_node的第一个参数是对non-const的引用Node 而函数 get_node return 是一个 Node by value。这意味着 调用表达式 get_node(2, &node1) 是一个 rvalue。但是因为,我们不能将 reference to non-const Node 绑定到类型 Nodervalue,你得到了提到的错误。

解决此问题的一种方法是将 get_node 的 return 类型更改为 Node&,如下所示:

//--vvvvv------------------------------------------------>return type changed to Node&
    Node& get_node(size_t position, Node *current_node){
        //other code as before
        return *current_node;
    }