Note that there are some explanatory texts on larger screens.

plurals
  1. POCan't call method with a pointer to an abstract class in C++
    primarykey
    data
    text
    <p>In my code I have an unordered map declared like this:</p> <pre><code>std::unordered_map&lt;Collider*, IObject*&gt; map; </code></pre> <p>where <code>IObject</code> is an abstract class that only contains pure virtual functions. After extracting an <code>IObject</code> pointer like this:</p> <pre><code>auto it = map.find(key); </code></pre> <p>where <code>key</code> is a <code>Collider*</code>, I try to access a member function of <code>IObject</code>:</p> <p><code>it-&gt;second-&gt;on_collision();</code> but Intellisense tells me that <code>IObject</code> has no members. Any ideas on what's wrong and possible solutions?</p> <p><strong>EDIT</strong></p> <p>For the curious, here is the class definition of my <code>IObject</code> class:</p> <pre><code>#ifndef IOBJECT_H_ #define IOBJECT_H_ class IObject { public: virtual const char* get_name() const = 0; virtual void set_name(const char* new_name) = 0; virtual void on_collision(const IObject&amp; other) = 0; virtual void update() = 0; }; #endif </code></pre> <p><strong>EDIT2</strong></p> <p>A short SSCCE for y'all, the key I believe is unimportant so I made an int.</p> <pre><code>#include &lt;unordered_map&gt; class IObject { public: virtual void on_collision() = 0; }; class PhysicsController { private: std::unordered_map&lt;int, IObject*&gt; map; public: void check_collisions(); }; void PhysicsController::check_collisions() { int key = 0; auto it = map.find(key); if(it != map.end()) { /* This is the offending line */ it-&gt;second-&gt;on_collision(); } } </code></pre> <p><strong>ANSWER</strong></p> <p>I found out the problem was in fact an issue with forward declarations. Thanks for the help guys</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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