Std iterator as private member variable
I have the following header file:
Code:
#include <list>
using namespace std;
template <class T>
class QuickList {
public:
// Some public things
private:
list<T> _theList;
mutable list<T>::iterator _currentPointer;
};
When I try to compile, I get the following errors (line numbers changed to correspond with the code above:
Quote:
In file included from quicklist.cpp:1:
quicklist.h:11: error: type `std::list<T, std::allocator<_CharT> >' is not derived from type `QuickList<T>'
quicklist.h:11: error: ISO C++ forbids declaration of `iterator' with no type
quicklist.h:11: error: expected `;' before "_currentPointer"
I need the mutable keyword if I want to keep my consts intact. However, if I remove it, I still get this single error:
Quote:
In file included from quicklist.cpp:1:
quicklist.h:11: error: expected `;' before "_currentPointer"
I don't understand these errors. What am I doing wrong?
Could someone help me with this?
Thanks!
Re: Std iterator as private member variable
What is in the quicklist.cpp file? One common problem with templates is that definition AND declaration need to be in de .h file. I think you must move your implementation from the .cpp file to the .h file
Re: Std iterator as private member variable
I am not sure about mutable but you need to use typename:
mutable typename list<T>::iterator _currentPointer;
will compile
Re: Std iterator as private member variable
typename, huh? I've never seen that one before. But it works! With mutable and everything. Thanks!
When exactly does one use typename?
And edb, I am aware of that problem. But apparently g++ has solved it. Because it now compiles perfectly. (And if, later, it seems not to work, I'll just move everything to the headerfile again.)
Re: Std iterator as private member variable
The compiler needs to know without any ambiguity that list<T>::iterator is the name of a type and not something else. Inside std::list, iterator could be something else than a type. You can find litterature on that topic in most C++ books; Stroustrup in C++ Programing Language Section C.13.5 for instance.
Re: Std iterator as private member variable
Ah, because T has not yet been determined. I suppose someone could create a totally seperate std::list<int> class in which iterator is not a type.
Thanks.