对“节点”的构造函数的调用不明确

问题描述 投票:0回答:1

我正在从教科书中执行链接列表的实现,并且收到错误消息:“在我的LinkedList.cpp中,此私有函数中对“节点”的构造函数的调用不明确:

void P1LinkedList::init(){
    theSize = 0;
    head = new Node;    //causes error
    tail = new Node;    //causes error
    head->next = tail; 
    tail->prev = head; 
} 

这是我的Node.h:

#ifndef Node_h
#define Node_h
struct Node{

        int data; 
        Node* next; 
        Node* prev; 

        Node(const int & d = 0, Node *p = nullptr, Node *n = nullptr); 
        Node(int && d = 0, Node *p = nullptr, Node *n = nullptr); 
};
#endif

还有我的Node.cpp:

#include "Node.h"
#include<iostream>

Node::Node(const int & d, Node *p, Node *n)
    :data{d}, prev{p}, next{n}{}

Node::Node(int && d, Node *p, Node *n)
    :data{std::move(d)}, prev{p}, next{n}{}

我猜测这与我编写Node构造函数的方式有关,但是我是根据教科书的大纲编写的,因此我不确定自己做错了什么。

c++ data-structures linked-list nodes
1个回答
0
投票

您已经编写了两个构造函数,都可以使用一个空的参数列表来调用它们。结果,编译器无法知道您打算调用哪个构造函数,也无法调用。

解决方案:确定要调用的构造函数,并确保另一个不能通过这些参数调用(在这种情况下,没有参数)。

© www.soinside.com 2019 - 2024. All rights reserved.