PDA

View Full Version : Std iterator as private member variable



Michiel
21st April 2006, 15:12
I have the following header file:


#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:


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:


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!

edb
21st April 2006, 16:06
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

jcr
21st April 2006, 16:06
I am not sure about mutable but you need to use typename:
mutable typename list<T>::iterator _currentPointer;
will compile

Michiel
21st April 2006, 16:10
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.)

jcr
21st April 2006, 16:22
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.

Michiel
21st April 2006, 16:27
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.