嵌套类“不命名类型”

问题描述 投票:1回答:1

我遇到以下错误:

main.cpp:18:5: error: 'Iterator' does not name a type
   18 |     Iterator begin() {
      |     ^~~~~~~~

使用此代码:

#include <iostream>
#include <iostream>
#include <memory>
#include <fstream>
#include <filesystem>

using namespace std;

class Numbers {
    private:
    int current;
    int end;

    public:

    Numbers(int end) : current(0), end(end) {}

    Iterator begin() {
        return Iterator(this);
    }

    bool operator==(const Numbers& other) const {
        return current == other.current && end == other.end;
    }

    bool operator!=(const Numbers& other) const {
        return !(other == *this);
    }

    class Iterator {
        private:
        Numbers* range;

        public:
        using value_type = int;
        using difference_type = ptrdiff_t;
        using pointer = int*;
        using reference = int&;
        using iterator_category = input_iterator_tag;

        Iterator(Numbers* range) : range(range) {}

        int operator*() const {
            return range->current;
        }

        int* operator->() const {
            return &range->current;
        }

        bool operator==(const Iterator& other) const {
            return other.range == range;
        }

        bool operator!=(const Iterator& other) const {
            return !(*this == other);
        }

        Iterator& operator++() {
            range->current++;
            return *this;
        }


    };
};

事实证明,移动begin函数under嵌套的Iterator类可以进行编译。

但是很奇怪-嵌套类不遵循与任何其他成员相同的访问规则,这意味着不需要前向引用吗?

我在网站上搜索了与此确切问题有关的其他问题,似乎没有找到答案。

c++ syntax nested
1个回答
0
投票

从评论到问题

也-我在成员函数中调用成员函数f没有问题q,其中f在q之后定义。你能解释为什么后面的例子与本问题描述的情况有所不同?

© www.soinside.com 2019 - 2024. All rights reserved.