Hi, i have an exercise to implement my own list class based on previously created Link class so that in this list class would be iterators and begin() and end() member functions. I will also add my Link class just in case but that is working as far as i know.
Of course I faced a problem when dong this exercise.
When I try to compile this part (lines 30 - 35)
Error 3 error C2509: 'begin' : member function not declared in 'list<Elem>'
Error 2 error C2143: syntax error : missing ';' before 'list<Elem>::begin'
Error 4 error C1903: unable to recover from previous error(s); stopping compilation
The problem is on line 13. That's a forward reference only. On line 39 you try to use an instance of the forward reference which fails. You need to define iterator (line 39) before you can actually instantiate it (line 30).
template<class Elem>
class list{
//representation and implementation details
Link<Elem>* p = nullptr;
public:
list() = default;
class iterator;
iterator begin();
iterator end();
iterator insert(iterator p, const Elem& v); //insert v into list after p
iterator erase(iterator p);
void push_back(const Elem& v); // insert v at end
void push_front(const Elem& v); // Insert v at front
void pop_front(); //remove 1st element
void pop_back(); //remove last element
Elem& front(); // the 1st element
Elem& back(); // the last element
};
template<class Elem>
class list<Elem>::iterator{
Link<Elem>* curr; //current link
public:
iterator(Link<Elem>* p) : curr{ p }{}
iterator& operator++(){ curr = curr->succ; return *this; }
iterator& operator--(){ curr = curr->prev; return *this; }
Elem& operator*(){ return curr->value; } //get value
booloperator==(const iterator& b) const { return curr == b.curr; }
booloperator!=(const iterator& b) const{ return curr != b.curr; }
};
template<class Elem>
list<Elem>::iterator list<Elem>::begin(){
auto p2 = go_to_first_elem(this->p);
list<Elem>::iterator iter(p2);
return iter;
}
I was still getting the same errors.
Did u mean i have to place all the iterator code right inside my list class? I haven't done this with classes but I imagine this should be similar like with function declarations. I declare that there is going to be this class and i can start using it in my code.
I could probably make this (iterator) class outside of list class but what if i don't want to use it outside of link class?
Anyway back to you answer. As far as i understood i had to define this iterator class before i started using it and now i switched definition of it before the code where i started using it. Why is it still giving the same results?
That the compiler doesn't recognize the definiton of iterator has to do with template and that it is actually defined outside the class (the compiler ignores the definition until it's too late).
So yes, if you put the definition of iterator inside the class it should work.
I imagine this should be similar like with function declarations.
Sorry for all the questions but is there a way to make this work if i leave the declaration inside list as it is, and write definition some other way so that compiler sees it and can use it when it needs?