I'm getting the following error:
main.cpp:18:5: error: 'Iterator' does not name a type
18 | Iterator begin() {
| ^~~~~~~~
With this code:
#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;
}
};
};
It turns out that moving the begin
function under the nested Iterator
class makes this compile.
But it's odd - don't nested classes follow the same access rules as any other member, meaning no need for forward-references?
I searched the other questions on the site regarding this exact issue, didn't seem to find an answer.