I’m relatively new to using C++ std objects, and am encountering a surprising error. I am trying to use a std::map to look up objects based on ID. It seems more efficient to store pointers to objects in the map, rather than the objects itself, so I preferred to use the first definition below, rather than the second. However, I’m getting a surprising error.
The following fails with error ‘template argument is invalid’
std::map<int, *cOutQueueItem> map_IndexedOutputItems;
The following code compiles fine.
std::map<int, cOutQueueItem> map_IndexedOutputItems;
Is the first one generally not allowed, or am I likely doing something else wrong?
Thanks!
>Solution :
The issue you’re encountering is due to incorrect syntax in your map declaration. When you want to store pointers in a std::map, the correct way to define it is by using the pointer type as the mapped value. The syntax cOutQueueItem* is incorrect in this context. Instead, you should use cOutQueueItem*.
The correct syntax should be –
std::map<int, cOutQueueItem*> map_IndexedOutputItems;