List
두 개의 중첩 클래스 가있는 클래스 가 있습니다 : ListItem
및 ListIterator
. 에서 ListIterator
클래스 I는 유형의 속성을 가지고 ListItem
. 헤더 파일 "List.h" ListItem
에서를 클래스의 개인 속성으로 정의했으며 ListIterator
완벽하게 컴파일되었습니다. 그러나 "List.cpp"파일에서 생성자를 정의하려고 ListIterator
하면 오류가 발생합니다.
List.cpp: In constructor ‘List::ListIterator::ListIterator(bool)’:
List.cpp:24:46: error: no matching function for call to ‘List::ListItem::ListItem()’
List::ListIterator::ListIterator(bool reverse){
^
List.cpp:24:46: note: candidates are:
List.cpp:7:1: note: List::ListItem::ListItem(void*)
List::ListItem::ListItem(void* data){
^
List.cpp:7:1: note: candidate expects 1 argument, 0 provided
In file included from List.cpp:1:0:
List.h:2:8: note: List::ListItem::ListItem(const List::ListItem&)
class ListItem {
^
List.h:2:8: note: candidate expects 1 argument, 0 provided
내 List.h 파일은 다음과 같습니다.
class List{
class ListItem {
public:
ListItem(void*);
void* getData();
ListItem* getNext();
ListItem* getPrev();
private:
void* data;
ListItem* next;
ListItem* prev;
};
class ListIterator {
public:
ListIterator(bool);
bool hasNext();
void* next();
private:
bool reverse;
ListItem current;
};
public:
List();
~List();
long getSize();
int addData(void*);
void* remove(long);
long indexOf(void*);
ListIterator* cellAt(long);
ListIterator& iterator(bool);
private:
long size;
ListItem head;
ListItem tail;
};
그리고 여기에 내 List.cpp 파일이 있습니다.
#include "List.h"
#include <iostream>
using namespace std;
/* ListItem */
List::ListItem::ListItem(void* data){
this->data = data;
}
void* List::ListItem::getData(){
return this->data;
}
List::ListItem* List::ListItem::getNext(){
return this->next;
}
List::ListItem* List::ListItem::getPrev(){
return this->prev;
}
/* ListIterator */
List::ListIterator::ListIterator(bool reverse){
this->reverse = reverse;
}
bool List::ListIterator::hasNext(){
return false;
}
void* List::ListIterator::next(){
return NULL;
}
문제는 클래스 중첩이 아니라 멤버 초기화에 관한 것입니다.
를 빌드 할 때 ListIterator
멤버도 빌드하므로 ListItem
, 사용자 정의가 하나 있으므로 ctor 호출을 지정해야합니다.
List::ListIterator::ListIterator(bool b) : current(something) {
...
}
current
초기화 방법을 표현하는 방법 입니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다