Iterator inheritance

H

Henrik Goldman

Using std::map I'm creating a class which adds a thread safety layer on top
of map.
However on some compilers I receive compiler errors which are not easily
decipherable.

I'm pretty sure that it's not a compiler specific problem but rather a lack
of understaning on my part of how to do the deriving correctly.

Here is the short version of the problem:

#include <map>

using namespace std;

template <class T, class U>

class CMap

{

public:

virtual ~CMap() {}

typedef typename map<T,U>::value_type value_type;

typedef typename map<T,U>::iterator iterator;



.... omitted code

protected:

map<T, U> m_Map;

};



template <class T, class U>

class CThrSafeMap: public CMap<T, U>

{

public:

virtual iterator Insert(const value_type &V) // <<---- problem is here

{

CMutexGuard MtxGuard(&m_Mutex);

return m_Map.insert(V).first;

}



While it works perfectly fine with Visual Studio I'm receiving errors with
g++ 3.4.6:

.../../shared/generic/mymap.h:51: error: `iterator' does not name a type
.../../shared/generic/mymap.h:51: error: (perhaps `typename CMap<T,
U>::iterator' was intended)
.../../shared/generic/mymap.h: In member function `virtual bool
CThrSafeMap<T, U>::IsEmpty()':
.../../shared/generic/mymap.h:62: error: `m_Map' was not declared in this
scope


What am I missing here?

Should I go further with the typename keyword?

Thanks.

-- Henrik
 
V

Victor Bazarov

Henrik said:
[..]
template <class T, class U>

class CThrSafeMap: public CMap<T, U>

{

public:

virtual iterator Insert(const value_type &V) // <<---- problem is
here

How many times?!...

virtual typename iterator Insert( ...

Isn't this already in the FAQ?
{

CMutexGuard MtxGuard(&m_Mutex);

return m_Map.insert(V).first;

}


[..]

V
 
A

Alf P. Steinbach

* Henrik Goldman:
Using std::map I'm creating a class which adds a thread safety layer on top
of map.
However on some compilers I receive compiler errors which are not easily
decipherable.

I'm pretty sure that it's not a compiler specific problem but rather a lack
of understaning on my part of how to do the deriving correctly.

Here is the short version of the problem:

#include <map>

using namespace std;

template <class T, class U>

class CMap

{

public:

virtual ~CMap() {}

typedef typename map<T,U>::value_type value_type;

typedef typename map<T,U>::iterator iterator;



... omitted code

protected:

map<T, U> m_Map;

};



template <class T, class U>

class CThrSafeMap: public CMap<T, U>

{

public:

virtual iterator Insert(const value_type &V) // <<---- problem is here

{

CMutexGuard MtxGuard(&m_Mutex);

return m_Map.insert(V).first;

}



While it works perfectly fine with Visual Studio I'm receiving errors with
g++ 3.4.6:

../../shared/generic/mymap.h:51: error: `iterator' does not name a type
../../shared/generic/mymap.h:51: error: (perhaps `typename CMap<T,
U>::iterator' was intended)
../../shared/generic/mymap.h: In member function `virtual bool
CThrSafeMap<T, U>::IsEmpty()':
../../shared/generic/mymap.h:62: error: `m_Map' was not declared in this
scope


What am I missing here?

Should I go further with the typename keyword?

Yes and no. You just need to refer to the base class' typedef. It
might seem like the language is not perfectly designed in this respect
(and I would agree!), but, what you need is an abundance of redundance:

template< classs T, class U >
class TreadSafeMap: public Map<T, U>
{
public:
typedef Map<T, U> Base;
typedef typename Base::value_type value_type;
typedef typename Base::iterator iterator;

//...
};

Cheers, & hth.,

- Alf
 
H

Henrik Goldman

How many times?!...

Not nearly enough! :)
virtual typename iterator Insert( ...

Your suggestion may be valid from a C++ language standpoint.
However the compiler doesn't accept it anyway:

.../../shared/generic/xfmap.h:51: error: expected nested-name-specifier
before "iterator"
.../../shared/generic/xfmap.h:51: error: `iterator' does not name a type
.../../shared/generic/xfmap.h:51: error: (perhaps `typename CMap<T,
U>::iterator' was intended)

-- Henrik
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top