By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,778 Members | 1,957 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,778 IT Pros & Developers. It's quick & easy.

what am i doing wrong?

P: n/a
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;
}
}

Jul 23 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
John wrote:
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
returns std::map<K,I>::const_iterator.
{
// The proble is the line below
dic_item tite = tmap.find(k);
//if(tit == tmap.end()) return (dic_item)nil;
return nil;
}
}


Jul 23 '05 #2

P: n/a
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

Jul 23 '05 #3

P: n/a
John wrote:
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_iter ator¬*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.

Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.