what am i doing wrong?

J

John

I am trying to implement a wrapper on the map
container with no luck. Seems like I'm messing up
something const/non-const but cant figure out the
error...would appreciate ur comments...



Line 40 is [ dic_item tite = tmap.find(k); ]

Error Message:

.../incl/mapdict.hpp:40: error: conversion from `
std::_Rb_tree_iterator<std::pair<const int, int>, const
std::pair<const int,
int>&, const std::pair<const int, int>*>' to non-scalar type `
std::_Rb_tree_iterator<std::pair<const int, int>, std::pair<const
int,
int>&, std::pair<const int, int>*>' requested

-------------simplified code

template <typename K, typename I>
class mapdict
{
std::map<K,I> tmap;


public:

typedef typename std::map<K,I>::iterator dic_item;

dic_item lookup(const K& k) const
{
// The proble is the line below
dic_item tite = tmap.find(k);
//if(tit == tmap.end()) return (dic_item)nil;
return nil;
}


}
 
R

Rolf Magnus

John said:
I am trying to implement a wrapper on the map
container with no luck. Seems like I'm messing up
something const/non-const but cant figure out the
error...would appreciate ur comments...



Line 40 is [ dic_item tite = tmap.find(k); ]

Error Message:

../incl/mapdict.hpp:40: error: conversion from `
std::_Rb_tree_iterator<std::pair<const int, int>, const
std::pair<const int,
int>&, const std::pair<const int, int>*>' to non-scalar type `
std::_Rb_tree_iterator<std::pair<const int, int>, std::pair<const
int,
int>&, std::pair<const int, int>*>' requested

-------------simplified code

template <typename K, typename I>
class mapdict
{
std::map<K,I> tmap;


public:

typedef typename std::map<K,I>::iterator dic_item;

dic_item lookup(const K& k) const
^^^^^

Your function is const, so the const version of find() is used, which
 
J

John

Thanks a lot. Indeed that was the problem. I got around it but not the
way I would have liked to. I was trying to const_cast my way around,
but it would not let me. The only way I can do it is by changing the
function
definition to

dic_item lookup(const K& k)

from

dic_item lookup(const K& k) const .


Is there any way to const_cast the line "dic_item tite = tmap.find(k);
"
I tried

dic_item tite = const_cast<dic_item>( tmap.find(k) );
dic_item tite = const_cast<dic_item>( tmap.find( const_cast<K&>(k) ) );


No luck :(

Thanks for your help,
--j
 
R

Rolf Magnus

John said:
Thanks a lot. Indeed that was the problem. I got around it but not the
way I would have liked to. I was trying to const_cast my way around,
but it would not let me. The only way I can do it is by changing the
function
definition to

dic_item lookup(const K& k)

from

dic_item lookup(const K& k) const .


Is there any way to const_cast the line "dic_item tite = tmap.find(k);
"
I tried

dic_item tite = const_cast<dic_item>( tmap.find(k) );
dic_item tite = const_cast<dic_item>( tmap.find( const_cast<K&>(k) ) );


No luck :(

That's because find doesn't return a const iterator, but a
const_iterator, i.e. not an iterator that is constant, but an iterator
into a constant map. You cannot const_cast a const_iterator into an
iterator. And you shouldn't really use const_cast here anyway. If your
member function is const, it means that function doesn't change the
object (of which the map is a part), so what's wrong with using the
const_iterator directly.

   typedef typename std::map<K,I>::const_iterator const_dic_item;

There seems to be something wrong with your design if the user of your class
needs to modify parts of a constant object.

There would be a way with const_cast (casting the map, not the iterator),
but I strongly advice against it.
 

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

Forum statistics

Threads
473,776
Messages
2,569,602
Members
45,185
Latest member
GluceaReviews

Latest Threads

Top