473,799 Members | 3,638 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_iterato r<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>::isC onst };
};
}

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

typedef utl::IF< utl::TypeTraits <typename
MapIterator::re ference>::isCon st,
const typename MapIterator::va lue_type::secon d_type&,
typename MapIterator::va lue_type::secon d_type&>::Resul t reference;

typedef utl::IF< utl::TypeTraits <typename
MapIterator::po inter>::isConst ,
typename MapIterator::va lue_type::secon d_type* const,
typename MapIterator::va lue_type::secon d_type*>::Resul t pointer;

ValueIterator(c onst MapIterator& mIter) : MapIterator(mIt er)
{ }

reference operator*() // *************** ERROR HERE *************
{
return MapIterator::op erator*().secon d;
}

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

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

typedef utl::IF< utl::TypeTraits <typename
MapIterator::re ference>::isCon st,
const typename MapIterator::va lue_type::first _type&,
typename MapIterator::va lue_type::first _type&>::Result reference;

typedef utl::IF< utl::TypeTraits <typename
MapIterator::po inter>::isConst ,
typename MapIterator::va lue_type::first _type* const,
typename MapIterator::va lue_type::first _type*>::Result pointer;

KeyIterator(con st MapIterator& mIter) : MapIterator(mIt er)
{ }

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

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

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

template <class MapIterator>
inline
KeyIterator<Map Iterator> keyIter(const MapIterator& iter)
{
return KeyIterator<Map Iterator>(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()(Obje ct* a)
{
cout << a->i << " ";
}
};

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

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

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

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

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

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

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

cout << endl;
cout << "Keys:\n";
copy( keyIter(m_Map.b egin()), keyIter(m_Map.e nd()),
ostream_iterato r<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*>::itera tor 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(vit er))->i << " "; // ************ COMPILER
ERROR ***************
}
map<int, Object*>::itera tor viter2 = m_Map.begin();
for ( viter2; viter2 != m_Map.end(); ++viter2 )
{
cout << (*valueIter(vit er2))->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 3399

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

Similar topics

3
2285
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 the map, which means I must iterate through all of the sets in the map. I need an iterator that points to the set where the integer was found. How can I do this? What will the type of this iterator be? Can you help me with a code snippet?
6
6663
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 give me this capability. So I want std::list<T*>::iterator. However, the container is of type std::list<T*>. How to get std::list<const T*>::iterator?
18
2570
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 std::vector<Entry>::iterator modelIterator; In a method, I am currently writing, I need to get a pointer to an entry in the vector.. I thought of the following:
0
2681
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 looked for one on the net (I did browse the boost library though). It doesn't matter right now, anyway. To put it simply, Abstract Iterator is mainly a wrapper class. It helps
16
2590
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 totally_isolated(Iterator& it) { //how do I find out if it points to the end node? }
27
5322
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 iterator. I have: class Parrot(object): def __iter__(self): return self def __init__(self): self.next = self._next()
3
2561
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()) { unit = hash.getNextUnit(); }
2
3064
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 point - this could be a custom hashmap (as opposed to rb-tree map that stl provides ). #include <string> #include <map>
3
1583
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 (nList<T>::Link). The Link class is defined inside of a template. (I assume that is possible?). Any one know what I doing wrong? Many Thanks, Purush Error from Compiler (gcc 3.4.6) nList.cc:52: error: expected `;' before "current_" *** Error code 1
0
9687
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10485
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10252
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10027
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7565
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6805
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5463
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4141
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.