Note that there are some explanatory texts on larger screens.

plurals
  1. POError in templates in probably wrong designed resource manager
    text
    copied!<p>I'm creating my first game. It will be Pacman or Snake. I'm going to use DirectX 11.</p> <p>I'm writing resource manager at the moment. I want to create it the most simple to use but I think that it is not well designed. Here's what I wrote:</p> <pre><code>#pragma once class Shader; class Mesh; class Texture; typedef std::map&lt;std::string, std::auto_ptr&lt;Shader&gt;&gt; shaders_map; typedef std::map&lt;std::string, std::auto_ptr&lt;Mesh&gt;&gt; meshes_map; typedef std::map&lt;std::string, std::auto_ptr&lt;Texture&gt;&gt; textures_map; template&lt;class C, class E&gt; inline int findElementInMap(const C&amp; cont, E *ptr) { C::const_iterator it = cont.begin(); int i = 0; while(it != cont.end()) { if(it-&gt;second.get() == ptr) // ERROR AT THIS LINE!!!! return i; i++; it++; } return -1; } class ResourceManager { public: ResourceManager(void); ~ResourceManager(void); template&lt;class T&gt; inline T* get(const std::string &amp;name) { if(typeid(T) == typeid(Shader)) { return (T*)getShader(name); } else if(typeid(T) == typeid(Mesh)) { return (T*)getMesh(name); } else if(typeid(T) == typeid(Texture)) { return (T*)getTexture(name); } return nullptr; } Shader* getShader(const std::string &amp;name); Mesh* getMesh(const std::string &amp;name); Texture* getTexture(const std::string &amp;name); template&lt;class T&gt; inline bool add(T *ptr) { if(typeid(T) == typeid(Shader)) { return addShader((Shader*)((void*)ptr)); } else if(typeid(T) == typeid(Mesh)) { return addMesh((Mesh*)((void*)ptr)); } else if(typeid(T) == typeid(Texture)) { return addTexture((Texture*)((void*)ptr)); } return false; } bool addShader(Shader *ptr); bool addMesh(Mesh *ptr); bool addTexture(Texture *ptr); template&lt;class E&gt; inline void release(E *ptr) { if(typeid(E) == typeid(Shader)) { release&lt;shaders_map, E&gt;(shaders, (E*)((void*)ptr)); return; } else if(typeid(E) == typeid(Mesh)) { release&lt;meshes_map, E&gt;(meshes, (E*)((void*)ptr)); return; } else if(typeid(E) == typeid(Texture)) { release&lt;textures_map, E&gt;(textures, ptr); } } // THIS METHOD CAUSES PROBLEM template&lt;class C, class E&gt; void release(C &amp;container, E *ptr) { assert(ptr != nullptr); int index = findElementInMap&lt;C, E&gt;(container, ptr); if(index &lt; 0) return; C::iterator it = container.begin(); it-&gt;second.release(); } private: shaders_map shaders; meshes_map meshes; textures_map textures; }; </code></pre> <p>And now compile error:</p> <pre><code>error C2440: '==' : cannot convert from 'Shader *' to 'Mesh *' int findElementInMap&lt;C,E&gt;(const C &amp;,E *)' being compiled with [ C=textures_map, E=Shader ] </code></pre> <p>So the container type and the element type don't match. Any ideas on how to set it work?</p> <p>Or should I build new resource manager from scratch?</p> <p>Edit: That's how I use this class:</p> <pre><code> Shader *sh = new Shader(); resourceManager.add&lt;Shader&gt;(sh); resourceManager.release&lt;Shader&gt;(sh); </code></pre>
 

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