Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

C++ std::map, can't use class pointer?

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading