cngkaygusuz
cngkaygusuz

Reputation: 1516

Trouble with STL map and pointers

typedef map<char,string> someMap;
someMap *mapPtr=someClass.getMap();
*(mapPtr)["a"].length();

The last line of this code fails. What should I to in order to make this work?

Upvotes: 2

Views: 105

Answers (2)

SirGuy
SirGuy

Reputation: 10780

    (*mapPtr)['a'].length();

the * operator has a lower precedence than [] so you have to but that in brackets. Also "a" is the string literal (char array) whereas you want 'a'

A complete list of operator precedences in C++ can be found here

Upvotes: 3

Pubby
Pubby

Reputation: 53097

[] has higher precedence than *

This is likely what you intend:

(*mapPtr)["a"].length();

Upvotes: 1

Related Questions