Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This code works (compiles &amp; runs) for me:</p> <pre><code>#include &lt;map&gt; class MyObject { }; int main(void) { typedef std::map&lt;int, MyObject*&gt; MyMap; MyMap *myMap = new MyMap; MyObject *obj = new MyObject; myMap-&gt;insert(MyMap::value_type(0, obj)); delete obj; delete myMap; } </code></pre> <p>So the problem lies in the details (<code>// ...</code> or what <code>MyObject</code> can do) or elsewhere. You can probably fix things up a bit to help. Try to stack allocate things when you can. Do you actually need a pointer to a map? I suggest you don't:</p> <pre><code>#include &lt;map&gt; class MyObject { }; int main(void) { typedef std::map&lt;int, MyObject*&gt; MyMap; MyMap myMap; MyObject *obj = new MyObject; myMap.insert(MyMap::value_type(0, obj)); delete obj; } </code></pre> <p>And do you actually need to store pointers to object, or objects? </p> <pre><code>#include &lt;map&gt; class MyObject { }; int main(void) { typedef std::map&lt;int, MyObject&gt; MyMap; MyMap myMap; myMap.insert(MyMap::value_type(0, MyObject())); } </code></pre> <p>Much smaller, and almost impossible to get memory leaks. If you <em>do</em> need to store pointers, for polymorphic behavior, check out <a href="http://www.boost.org/doc/libs/1_39_0/libs/ptr_container/doc/headers.html" rel="noreferrer">boost::ptr_container library</a>, which has a <a href="http://www.boost.org/doc/libs/1_39_0/libs/ptr_container/doc/ptr_map_adapter.html" rel="noreferrer">map adapter that stores pointers</a>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload