In C++, how can I use a nested class type in an other nested class (the two nested classes are under the same outer class)

时间:2021-11-23 23:29:04

I have a class List which has two nested classes: ListItem and ListIterator. In the ListIterator class I have an attribute of the type ListItem. In the header file "List.h" I defined the ListItem as a private attribute of the class ListIterator and it compiled perfectly. But, in the "List.cpp" file, when I try to define the constructor of ListIterator it gives me the error:

我有一个类List,它有两个嵌套类:ListItem和ListIterator。在ListIterator类中,我有一个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

Here is my List.h file:

这是我的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;
};

And here is my List.cpp file:

这是我的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;
}

1 个解决方案

#1


1  

Your problem is not about class nesting but about members initialization.

您的问题不是关于类嵌套,而是关于成员初始化。

When building a ListIterator you are also building its members, so a ListItem, then you need to specify a ctor call for it as it has one user-defined:

在构建ListIterator时,你也在构建它的成员,所以是一个ListItem,然后你需要为它指定一个ctor调用,因为它有一个用户定义的:

List::ListIterator::ListIterator(bool b) : current(something) {
    ...
}

This is the way to express how current is initialized.

这是表达电流初始化方式的方法。

#1


1  

Your problem is not about class nesting but about members initialization.

您的问题不是关于类嵌套,而是关于成员初始化。

When building a ListIterator you are also building its members, so a ListItem, then you need to specify a ctor call for it as it has one user-defined:

在构建ListIterator时,你也在构建它的成员,所以是一个ListItem,然后你需要为它指定一个ctor调用,因为它有一个用户定义的:

List::ListIterator::ListIterator(bool b) : current(something) {
    ...
}

This is the way to express how current is initialized.

这是表达电流初始化方式的方法。