472,358 Members | 2,065 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

what am i doing wrong?

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
3 1717
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

125
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from...
121
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode...
11
by: Alfonso Morra | last post by:
Hi, I am at the end of my tether now - after spending several days trying to figure how to do this. I have finally written a simple "proof of concept" program to test serializing a structure...
46
by: Keith K | last post by:
Having developed with VB since 1992, I am now VERY interested in C#. I've written several applications with C# and I do enjoy the language. What C# Needs: There are a few things that I do...
13
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
2
by: Aaron Ackerman | last post by:
I cannot a row to this bound DataGrid to SAVE MY LIFE! I have tried everything and I am at a loss. The using goes into add mode with the add button adds his data then updates with the update...
16
by: Ajay | last post by:
Hi all, i want to know when i create a class.what all it contains.I know the following things are there by default if i do not declare them by myself.Please tell the other things that are left. ...
8
by: watkinsdev | last post by:
Hi, I have created a mesh class in visual studio 6.0 c++. I can create a device, render objects and can edit the objects by for instancnce selecting a cluster of vertices and processing the...
0
by: shapper | last post by:
Hello, I am creating a class with a control. I compiled the class and used it on an Asp.Net 2.0 web site page. I can see the begin and end tags of my control (<oland </ol>) but somehow the...
10
by: DavidSeck.com | last post by:
Hi, I am working with the Facebook API right now, an I have kind of a problem, but I don't know what I am doing wrong. So I have a few arrays, f.ex.: User albums: array(2) {
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
1
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. header("Location:".$urlback); Is this the right layout the...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...

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.