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

operator[] for maps

P: n/a
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
Share this Question
Share on Google+
3 Replies


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

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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.