Reputation: 390
I can't figure out how to solve this problem. My iterator constructors need to be private, so I use the container to return an iterator, but it refuses to compile.
A.h
class A {
public:
class iterator {
friend class A;
public:
virtual ~iterator();
iterator operator++(int);
iterator operator--(int);
private:
iterator(int index, A container);
};
iterator begin();
iterator end();
};
A.cpp
A::iterator A::begin(){
return iterator(0, *this);
}
A::iterator A::end(){
return iterator(length(), *this);
}
I then call it like this:
for(A::iterator i = a.begin(); i != a.end(); i++)
std::cout << *i;
But that last block says that "A::iterator is private within this context". I have a feeling the iterator constructor is only being called outside of begin(), but I don't know how to fix it. I'm fairly new to C++, so any help would be appreciated!
Upvotes: 1
Views: 1081
Reputation: 409216
It's because the iterator
class doesn't have a public constructor. You should implement a public copy-constructor (and probably a copy-assignment operator (see the rule of three)).
Upvotes: 4