473,396 Members | 1,599 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,396 software developers and data experts.

std::map const troubles

I do not understand why this code fails to compile (under gcc):

#include <map>
using namespace std;

class Foo {
map<Foo*,int> myMap;
public:
int lookup(const Foo& f) const { myMap.find(&f); }
};

The error message states that I am discarding qualifiers by passing a
const map<...> to the map::find function. But there is a const
version of the map::find function. Why isn't the compiler using that
version?

If I replace "map<Foo*,int>" with "map<const Foo*,int>" or if I change
"lookup(const Foo& f)" with "lookup(Foo& f)" suddenly it works. Why?
I am befuddled.

How do I get this to work, without changing the signatures?
Jul 22 '05 #1
4 1409
I do not understand why this code fails to compile (under gcc):

#include <map>
using namespace std;

class Foo {
map<Foo*,int> myMap;
public:
int lookup(const Foo& f) const { myMap.find(&f); }
};

lookup it supposed to return an int and I dont see it returning one.

Raj
Jul 22 '05 #2

"Grey Plastic" <gr*********@hotmail.com> wrote in message
news:1d**************************@posting.google.c om...
I do not understand why this code fails to compile (under gcc):

#include <map>
using namespace std;

class Foo {
map<Foo*,int> myMap;
public:
int lookup(const Foo& f) const { myMap.find(&f); }
I assume you mean something like this

int lookup(const Foo& f) const { return myMap.find(&f)->second; }
};

The error message states that I am discarding qualifiers by passing a
const map<...> to the map::find function. But there is a const
version of the map::find function. Why isn't the compiler using that
version?
It is using the const version, but the const version takes a Foo* parameter
and you are giving it a const Foo* parameter.

If I replace "map<Foo*,int>" with "map<const Foo*,int>" or if I change
"lookup(const Foo& f)" with "lookup(Foo& f)" suddenly it works. Why?
See above.
I am befuddled.

How do I get this to work, without changing the signatures?


int lookup(Foo f) const { return myMap.find(&f)->second; }

That works, at the cost of copying a Foo object.

john
Jul 22 '05 #3
Grey Plastic wrote in
news:1d**************************@posting.google.c om in comp.lang.c++:
I do not understand why this code fails to compile (under gcc):

#include <map>
using namespace std;

class Foo {
map<Foo*,int> myMap;
public:
int lookup(const Foo& f) const { myMap.find(&f); }
};

The error message states that I am discarding qualifiers by passing a
const map<...> to the map::find function. But there is a const
version of the map::find function. Why isn't the compiler using that
version?

If I replace "map<Foo*,int>" with "map<const Foo*,int>" or if I change
"lookup(const Foo& f)" with "lookup(Foo& f)" suddenly it works. Why?
I am befuddled.

How do I get this to work, without changing the signatures?


The key type of the map is `Foo * const` your lookup() function passes
in a `Foo const *` this cast *requires* const_cast<>, as even though
you are adding a const qualifier, you are also removing one.

IOW:

Foo const * -> Foo const * const, is ok but you are doing
Foo const * -> Foo * -> Foo * const.

The good news is that since std::map doesn't dereference the
'key' you can safely use const_cast<>().
int lookup( Foo const &f )
{
myMap.find( const_cast< Foo * >( &f ) );
}

HTH.

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #4
> >
How do I get this to work, without changing the signatures?


int lookup(Foo f) const { return myMap.find(&f)->second; }

That works, at the cost of copying a Foo object.


It's rubbish. It compiles but it won't work because the address of the
object passed to find will be different from the address of the object used
to call lookup. Apologies.

john
Jul 22 '05 #5

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

Similar topics

24
by: Duane Hebert | last post by:
2 questions: Given a map defined as std::map<int,string> stringmap; //How do you access the data_type (string) via an iterator? std::string spoo("doh");
1
by: Antti Granqvist | last post by:
Hello! I have following object relations: Competition 1--* Category 1--* Course 1 | | * Course
44
by: jmoy | last post by:
I am a C programmer graduating to C++. As an exercise I wrote a program to count the number of times that different words occur in a text file. Though a hash table might have been a better choice,...
1
by: Bob | last post by:
Hi, I'm trying to use a map with a string key, and a pointer to objects contained in a vector. I've wrapped this in a class like so: // cMap template<class T> class cMap : public cList<T> { ...
2
by: Serengeti | last post by:
Hello, in my class I have a map that translates strings to pointers to some member functions. The code goes like this: class F { typedef void (Function::*MathFuncPtr)(); std::map<std::string,...
1
by: Saeed Amrollahi | last post by:
Dear All C++ Programmers Hello I am Saeed Amrollahi. I am a software engineer in Tehran Sewerage Company. I try to use std::map and map::find member function. I use Visual Studio .NET. my...
3
by: Dan Trowbridge | last post by:
Hi everyone, In my attempt to port code from VS 6.0 to VS.NET I had some code break along the way, mostly due to not adhereing closely to the C++ standard. This may be another instance but I...
13
by: kamaraj80 | last post by:
Hi I am using the std:: map as following. typedef struct _SeatRowCols { long nSeatRow; unsigned char ucSeatLetter; }SeatRowCols; typedef struct _NetData
2
by: mergaite_lietuvaite | last post by:
Hi, I try to use map. My code is very easy, but there are some errors.... (by compiling). Plaese, could you say, what I do wrong.. struct less_array { bool operator()(const FArray p1, const...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.