类没有构造函数,但它具有正确的参数

时间:2016-06-29 20:44:14

标签: c++ constructor

class Iterator;

class SortedList {

    friend class Iterator;

private:
    Node* root;

    Node* minim(Node* node) const {
        while (node->getLeft() != nullptr)
            node = node->getLeft();
        return node;
};

public:
    SortedList() {
        root = nullptr;
    };

    Iterator iterator() {
        Node* min = minim(root);
        return Iterator(*this, min); // Here says that Iterator has no constructors
    };
};  


class Iterator {
private:
    const SortedList& list;
    Node* current;

public:
    Iterator(const SortedList& list, Node* current) : list{ list }, current{ current } {};

};  

它表示在SortedList的迭代器方法中,Iterator类没有构造函数也没有说匹配与否,如果我修改了一些参数是不正确的,它确实指定没有任何这些参数

另一件事,如果我注释掉iterator方法并在main中实例化一个迭代器,通过编写Iterator {my parameters}它就可以了。

1 个答案:

答案 0 :(得分:2)

使用时

Iterator是不完整的类型:

Iterator iterator() {
    Node* min = minim(root);
    return Iterator(*this, min); // Here says that Iterator has no constructors
};

在定义Iterator之后移动函数的定义。

class Iterator;

class SortedList {

    friend class Iterator;

private:
    Node* root;

    Node* minim(Node* node) const {
        while (node->getLeft() != nullptr)
            node = node->getLeft();
        return node;
};

public:
    SortedList() {
        root = nullptr;
    };

    // Just the declaration
    Iterator iterator();
};  


class Iterator {
private:
    const SortedList& list;
    Node* current;

public:
    Iterator(const SortedList& list, Node* current) : list{ list }, current{ current } {};

};

// Now the definition    
Iterator SortedList::iterator() {
   Node* min = minim(root);
   return Iterator(*this, min);
};