【发布时间】:2019-04-30 11:40:07
【问题描述】:
在函数“迭代列表::begin()”中{ 这个迭代(头)有一个问题“没有匹配的初始化构造函数”。 head 是一个节点指针,我为它构建了一个构造函数。不知道是什么问题。
List.h
#include "Iteratoring.h"
struct Node {
int data; // value in the node
Node *next; // the address of the next node
/**************************************
** CONSTRUCTOR **
***************************************/
Node(int data) : data(data), next(0) {}
};
class List {
private:
Node *head= nullptr; // head node
Node *tail; // tail node
Iteratoring begin();
public:
};
List.cpp
#include "List.h"
Iteratoring List::begin() {
return Iteratoring(head); //The error is here. no matching constructor for initialization
}
Iteratoring.h
#include "List.h"
class Iteratoring {
private:
Node *current;
public:
Iteratoring(){
current= nullptr;
};
Iteratoring(Node *ptr){
current=ptr;
};
};
【问题讨论】:
-
这是标准的循环依赖问题:文件
List.h包含Iteratoring.h,其中包含List.h。您可以通过前向声明解决这个问题(我的建议是在Iteratoring.h文件中使用Node的前向声明,并删除包含List.h)。
标签: c++ linked-list iterator