如何在 C++ 中为迭代器 class 创建链表的构造函数?
How to create a constructor of linked list for iterator class in c++?
当我尝试 运行 此代码时,出现此错误:
constructor for 'Linkedlist' must explicitly initialize the member
'point' which does not have a default constructor
Linkedlist::Linkedlist()
class Node {
public:
Node* next;
Node* prev;
Elem elem;
friend class Linkedlist;
Node(): next(NULL), prev(NULL)
{}
Node(Elem elem) : elem(elem)
{}
};
class Iterator {
private:
Node* iter;
//Iterator(Node curr);
public:
friend class Linkedlist;
Iterator(Node* curr) {
iter=curr;
}
};
class Linkedlist {
private:
Node *head;
Node *tail;
int N;
Iterator point;
public:
Iterator point;
Linkedlist();
};
Linkedlist::Linkedlist() {
N = 0;
head = new Node();
tail = new Node();
head->next = tail;
tail->prev = head;
point.iter = head;
}
我不确定如何解决这个问题,感谢任何帮助!
您可以做几件事。您可以将其声明为指针
class Linkedlist { //Missing a c on your class declaration
private:
Node *head;
Node *tail;
int N;
//Iterator point; (You have this declared twice)
public:
Iterator *point;
Linkedlist();//s
在这种情况下,您需要像这样在 LinkedList 构造函数中调用 point 的构造函数。
Linkedlist::Linkedlist() {
N = 0;
head = new Node();
tail = new Node();
head->next = tail;
tail->prev = head;
point = new Iterator(head); // Use the existing constructor
}
或者,您可以为迭代器创建默认构造函数 class。
class Iterator {
private:
Node* iter;
public:
friend class Linkedlist;
Iterator() {
iter = 0;
}
Iterator(Node* curr) {
iter=curr;
}
};
最后,您可以使用此语法指示程序在为 LinkedList 分配内存之前使用空指针分配点。
Linkedlist::Linkedlist() : point(0) {
N = 0;
head = new Node();
tail = new Node();
head->next = tail;
tail->prev = head;
point.iter=head;
}
当我尝试 运行 此代码时,出现此错误:
constructor for 'Linkedlist' must explicitly initialize the member 'point' which does not have a default constructor Linkedlist::Linkedlist()
class Node {
public:
Node* next;
Node* prev;
Elem elem;
friend class Linkedlist;
Node(): next(NULL), prev(NULL)
{}
Node(Elem elem) : elem(elem)
{}
};
class Iterator {
private:
Node* iter;
//Iterator(Node curr);
public:
friend class Linkedlist;
Iterator(Node* curr) {
iter=curr;
}
};
class Linkedlist {
private:
Node *head;
Node *tail;
int N;
Iterator point;
public:
Iterator point;
Linkedlist();
};
Linkedlist::Linkedlist() {
N = 0;
head = new Node();
tail = new Node();
head->next = tail;
tail->prev = head;
point.iter = head;
}
我不确定如何解决这个问题,感谢任何帮助!
您可以做几件事。您可以将其声明为指针
class Linkedlist { //Missing a c on your class declaration
private:
Node *head;
Node *tail;
int N;
//Iterator point; (You have this declared twice)
public:
Iterator *point;
Linkedlist();//s
在这种情况下,您需要像这样在 LinkedList 构造函数中调用 point 的构造函数。
Linkedlist::Linkedlist() {
N = 0;
head = new Node();
tail = new Node();
head->next = tail;
tail->prev = head;
point = new Iterator(head); // Use the existing constructor
}
或者,您可以为迭代器创建默认构造函数 class。
class Iterator {
private:
Node* iter;
public:
friend class Linkedlist;
Iterator() {
iter = 0;
}
Iterator(Node* curr) {
iter=curr;
}
};
最后,您可以使用此语法指示程序在为 LinkedList 分配内存之前使用空指针分配点。
Linkedlist::Linkedlist() : point(0) {
N = 0;
head = new Node();
tail = new Node();
head->next = tail;
tail->prev = head;
point.iter=head;
}