[英]Call to constructor of 'Node' is ambiguous
我正在关注教科书中的链表实现,但收到错误“在 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 构造函数的方式有关,但我是根据教科书的大纲编写的,所以我不确定我做错了什么。
您已经编写了两个构造函数,它们都可以用空参数列表调用。 因此,编译器无法知道您打算调用哪个构造函数,也无法调用。
解决方案:决定您打算调用哪个构造函数,并确保另一个构造函数不可使用这些参数调用(在这种情况下,没有参数)。
在这种非常特殊的情况下,只需删除第二个构造函数。 它不会做第一个不会做的任何事情。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.