473,385 Members | 1,893 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Map Iterator is wrapped for keys and values - compiler error

I am trying to wrap the map iterator for keys and values. However, I seem to
run into problems with the values for const_iterator - it works for all
other combinations. Below I list my code and the compiler error. Please
help if possible. Thanks in advance.

// Compiler error
ra:/home/ssangapu/personal/C++/STL/mapIter-738>g++ main.cc
utlMapIterator.h: In method `class Object *&
ValueIterator<_Rb_tree_iterator<pair<const int,Object *>,const pair<const
int,Object *> &,const pair<const int,Object *> *> >::operator *()':

main.cc:84: instantiated from here
utlMapIterator.h:27: conversion from `Object *const' to `Object *&' discards
qualifiers
ra:/home/ssangapu/personal/C++/STL/mapIter-739>
//==================
// utlMapIterator.h
//==================
namespace utl
{
template <bool flag, typename ThenType, typename ElseType>
struct IF
{
typedef ThenType Result;
};

template <typename ThenType, typename ElseType>
struct IF<false, ThenType, ElseType>
{
typedef ElseType Result;
};

template <typename T>
class TypeTraits
{
private:
template<class U>
struct UnConst
{
typedef U Result;
enum { isConst = false };
};

template<class U>
struct UnConst<const U>
{
typedef U Result;
enum { isConst = true };
};

public:

enum { isConst = UnConst<T>::isConst };
};
}

template <class MapIterator>
class ValueIterator : public MapIterator
{
public:

typedef utl::IF< utl::TypeTraits<typename
MapIterator::reference>::isConst,
const typename MapIterator::value_type::second_type&,
typename MapIterator::value_type::second_type&>::Result reference;

typedef utl::IF< utl::TypeTraits<typename
MapIterator::pointer>::isConst,
typename MapIterator::value_type::second_type* const,
typename MapIterator::value_type::second_type*>::Result pointer;

ValueIterator(const MapIterator& mIter) : MapIterator(mIter)
{ }

reference operator*() // *************** ERROR HERE *************
{
return MapIterator::operator*().second;
}

pointer operator->()
{
return &**this;
}
};

template <class MapIterator>
class KeyIterator : public MapIterator
{
public:

typedef utl::IF< utl::TypeTraits<typename
MapIterator::reference>::isConst,
const typename MapIterator::value_type::first_type&,
typename MapIterator::value_type::first_type&>::Result reference;

typedef utl::IF< utl::TypeTraits<typename
MapIterator::pointer>::isConst,
typename MapIterator::value_type::first_type* const,
typename MapIterator::value_type::first_type*>::Result pointer;

KeyIterator(const MapIterator& mIter) : MapIterator(mIter)
{ }

reference operator*() const
{
return MapIterator::operator*().first;
}

pointer operator->() const
{
return &**this;
}
};

template <class MapIterator>
inline
ValueIterator<MapIterator> valueIter(const MapIterator& iter)
{
return ValueIterator<MapIterator>(iter);
}

template <class MapIterator>
inline
KeyIterator<MapIterator> keyIter(const MapIterator& iter)
{
return KeyIterator<MapIterator>(iter);
}
//==================
// main.cc
//==================

#include <iostream>
#include <string>
#include <map>
#include <list>
#include <functional>
#include <algorithm>
#include <iterator>
#include "utlMapIterator.h"

using namespace std;

class Object
{
public:
Object(int a) { i = a; }
int i;
};

class Print
{
public:
void operator()(Object* a)
{
cout << a->i << " ";
}
};

int main()
{
map<int, Object*> m_Map;
Object o1(1*2);
m_Map.insert(make_pair(1, &o1));

Object o2(2*2);
m_Map.insert(make_pair(2, &o2));

Object o3(3*2);
m_Map.insert(make_pair(3, &o3));

Object o4(4*2);
m_Map.insert(make_pair(4, &o4));

Object o5(5*2);
m_Map.insert(make_pair(5, &o5));

Object o6(6*2);
m_Map.insert(make_pair(6, &o6));

cout << &o1 << " " << &o2 << " " << &o3 << " ";
cout << &o4 << " " << &o5 << " " << &o6 << endl;

cout << endl;
cout << "Keys:\n";
copy( keyIter(m_Map.begin()), keyIter(m_Map.end()),
ostream_iterator<int>(cout, " ") );
cout << endl;

cout << "Values:\n";
for_each( valueIter(m_Map.begin()), valueIter(m_Map.end()), Print() );
cout << endl;
cout << endl;

cout << "Keys:\n";
map<int, Object*>::const_iterator iter = m_Map.begin();
for ( iter; iter != m_Map.end(); ++iter )
{
cout << *(keyIter(iter)) << " ";
}
cout << endl;
map<int, Object*>::iterator iter2 = m_Map.begin();
for ( iter2; iter2 != m_Map.end(); ++iter2 )
{
cout << *(keyIter(iter2)) << " ";
}
cout << endl;

cout << "Values:\n";
map<int, Object*>::const_iterator viter = m_Map.begin();
for ( viter; viter != m_Map.end(); ++viter )
{
cout << (*valueIter(viter))->i << " "; // ************ COMPILER
ERROR ***************
}
map<int, Object*>::iterator viter2 = m_Map.begin();
for ( viter2; viter2 != m_Map.end(); ++viter2 )
{
cout << (*valueIter(viter2))->i << " ";
}
cout << endl;
cout << endl;
cout << endl;

cout << "Copied to a list.\nValues:\n";
list<Object*> myList;
myList.insert( myList.end(),
valueIter(m_Map.begin()),
valueIter(m_Map.end()) );
for_each( myList.begin(), myList.end(), Print() );

cout << endl;
cout << endl;
}
Jul 19 '05 #1
0 3375

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: uclamathguy | last post by:
I am working on connected component analysis, but that is irrelevant. I have a mapping containing ints as the keys and sets of ints as the "values." Given an integer, I need to iterate through...
6
by: PengYu.UT | last post by:
Hi, Suppose I have a list which contains pointers. I want the pointer got by dereferencing the iterator be a pointer pointing to a const object. But std::list<const T*>::const_iterator doens't...
18
by: silversurfer | last post by:
Ok, this should be fairly easy for most of you (at least I hope so), but not for me: Let us say we have got the following elements: std::vector<Entry> models; //Entry is a struct...
0
by: mailforpr | last post by:
Hi. Let me introduce an iterator to you, the so-called "Abstract Iterator" I developed the other day. I actually have no idea if there's another "Abstract Iterator" out there, as I have never...
16
by: mailforpr | last post by:
How do I do that? The thing is, the only information I have about the iterator is the iterator itself. No container it is belonging to or anything. Like template<Iteratorvoid...
27
by: Steven D'Aprano | last post by:
I thought that an iterator was any object that follows the iterator protocol, that is, it has a next() method and an __iter__() method. But I'm having problems writing a class that acts as an...
3
by: raan | last post by:
Is iterators singleton? I am accessing a map through wrapper functions to it; that allow me to access the contents of the map as CUnit &unit = hash.getFirstunit(); while(hash.hasMoreunits())...
2
by: Rakesh Kumar | last post by:
I am encountering the following issue with STL map iterator - wrapped within a template. ( I am wrapping it withing a template since I want to hide the map implementation from the user . At a later...
3
by: pnayak | last post by:
Hi, I am trying to implement an iterator to a List class. The code is included below. I get the following compiler error. At the point where I use a class that is defined inside a template...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.