std::map and the heap
-
Hi, I have the following code:
typedef std::map MyMapType; MyMapType myMap; myMap\["abc"\] = "def";
This works as expected. But I want my map to be on the heap, so I tried this:
MyMapType \*myNewMap = new MyMapType; myNewMap\["abc"\] = "def"; // error C2107: illegal index, indirection not allowed \*myNewMap\["abc"\] = "def"; // error C2107: illegal index, indirection not allowed
The two lines are commented with the compile time errors it generates. What have I missed?
- Dy
-
Hi, I have the following code:
typedef std::map MyMapType; MyMapType myMap; myMap\["abc"\] = "def";
This works as expected. But I want my map to be on the heap, so I tried this:
MyMapType \*myNewMap = new MyMapType; myNewMap\["abc"\] = "def"; // error C2107: illegal index, indirection not allowed \*myNewMap\["abc"\] = "def"; // error C2107: illegal index, indirection not allowed
The two lines are commented with the compile time errors it generates. What have I missed?
- Dy
A pair of braces:
(*myNewMap)["abc"] = "def";
compiles well. BTW, you also missed to properly format your code (have a look at
typedef
statement in the OP). :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
-
A pair of braces:
(*myNewMap)["abc"] = "def";
compiles well. BTW, you also missed to properly format your code (have a look at
typedef
statement in the OP). :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.