473,406 Members | 2,378 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,406 software developers and data experts.

operator[] for maps

Hi all,

I'm having some problems with the compilation of the following example
due to a compile time error:
#include <iostream>
#include <map>

using namespace std;

class X {
public:

void insert(const string * id, unsigned int x) { strCodeMap[id] = x;
}
unsigned int getVarCode(const string *id) const { return
strCodeMap[id]; }

private:

struct ltstr {
bool operator()(const string * s1, const string * s2) const {
return *s1 < *s2;
}
};

map<const string *, unsigned int, ltstr> strCodeMap;
};

int main() {

X c;
const string * s1 = new string("Hello");
const string * s2 = new string("Buh");

c.insert(s1, 3);
c.insert(s2, 4);

const string * s4 = new string("Hello");

cout << "S4 VALUE: " << c.getVarCode(s4) << "\n";

delete s1;
delete s2;
delete s4;

return 0;
}

The compile time error is in the public method of class X:
unsigned int getVarCode(const string *id) const { return
strCodeMap[id]; }

And is:
$ g++ maptest.cc -o maptest
maptest.cc: In member function `unsigned int X::getVarCode(const
std::string*)
const':
maptest.cc:10: error: passing `const std::map<const std::string*,
unsigned int,
X::ltstr, std::allocator<std::pair<const std::string* const,
unsigned int> >
' as `this' argument of `_Tp& std::map<_Key, _Tp, _Compare,

_Alloc>::operator[](const _Key&) [with _Key = const std::string*,
_Tp =
unsigned int, _Compare = X::ltstr, _Alloc =
std::allocator<std::pair<const
std::string* const, unsigned int> >]' discards qualifiers

Can someone give me a hint on what this means?

Cheers,

Paulo Matos

Jul 23 '05 #1
3 1330
"pmatos" <po**@sat.inesc-id.pt> wrote in news:1121894249.213194.191780
@z14g2000cwz.googlegroups.com:
Hi all,

I'm having some problems with the compilation of the following example
due to a compile time error:
#include <iostream>
#include <map>

using namespace std;

class X {
public:

void insert(const string * id, unsigned int x) { strCodeMap[id] = x;
}
unsigned int getVarCode(const string *id) const { return
strCodeMap[id]; }
operator[] is a non-const method of std::map. So you can't call it from
a const method in your class (since strCodeMap is a member of your
class). Food for thought: what does getVarCode return if id doesn't
exist in the map?

private:

struct ltstr {
bool operator()(const string * s1, const string * s2) const {
return *s1 < *s2;
}
};

map<const string *, unsigned int, ltstr> strCodeMap;
};

Jul 23 '05 #2
pmatos wrote:
I'm having some problems with the compilation of the following example
due to a compile time error:
#include <iostream>
#include <map>

using namespace std;

class X {
public:

void insert(const string * id, unsigned int x) { strCodeMap[id] = x;
}
unsigned int getVarCode(const string *id) const { return
strCodeMap[id]; }
You cannot use [] on a const map. This member function is 'const', so
all the members are const.
[...]


V
Jul 23 '05 #3
Actually normally if you call operator[] on a map for an element that
doesn't exist, it creates the element with a default value and gives
you a reference to it. It can be a very useful way to look up and
element and insert if it doesn't exist (for example, a cache / lazy
evaluation).

You cannot use it for const lookup. Instead you should use an iterator
and the method find().
Of course you can define a const overload for operator[] in your own
class if you like.

The reasons they didn't make a const overload for operator[] in map
are:
- If the element doesn't exist, it cannot create one, so what would it
return you a reference to?
- If you were allowed to use this for reading, and you actually had a
non-const map it would have the side-effect of inserting an element
when you didn't want to.

Jul 23 '05 #4

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

Similar topics

3
by: Raymond Hettinger | last post by:
Since the advent of extended slicing, operator.isMappingType() returns a misleading result. >>> map(operator.isMappingType, (, (), {}, '', u'')) I recommend removing it from the operator...
6
by: Ney André de Mello Zunino | last post by:
Hello. Searching around before posting this message revealed that the issue has been brought up a significant number of times (here are two related threads ). So /std::map<>/ does not provide...
13
by: jstanforth | last post by:
This is probably a very obvious question, but I'm not clear on what operators need to be implemented for std::map.find() to work. For example, I have a class MyString that wraps std::string, and...
80
by: Christopher Benson-Manica | last post by:
Of course one can get the effect with appropriate use of existing operators, but a ^^ operator would make for nice symmetry (as well as useful to me in something I'm working on). Am I the only one...
2
by: Dennis | last post by:
What's the condition for returning true for the less than (<) operator between two maps? Is it just based on size? Or does the contents of the map matter and if so, how? Dennis
6
by: r.simoni | last post by:
Hi, i have seen that this operator returns a reference to TYPE and not a const reference. I have a problem and I can't solve it: i have a properties class with a get_property(const string &)...
5
by: raan | last post by:
What I am trying to achieve here is depicted in the small program below. // Wrapit.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <map> #include...
5
by: subramanian100in | last post by:
For inserting new elements in map, we can use insert member function. To know if an element exists or not in a map, we can use count or find member function. Also, we can use the iterator...
19
by: C++Liliput | last post by:
I have a custom String class that contains an embedded char* member. The copy constructor, assignment operator etc. are all correctly defined. I need to create a map of my string (say a class...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
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
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,...
0
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...

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.