Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8563283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T16:55:33+00:00 2026-06-11T16:55:33+00:00

I have a list of pointers to a base abstract class (Entity) std::list<Entity*> m_entities;

  • 0

I have a list of pointers to a base abstract class (Entity)

std::list<Entity*> m_entities;

I have created a typedef for iterating through this class

typedef std::list<Entity*>::const_iterator entityIter;

I then try and iterate through each pointer in the list

for (entityIter i = m_entities.begin(); i != m_entities.end(); ++i)
{
    const Entity &e = *i;    // ERROR
    e.DoStuff();
}

I get the following error when attempting to reference each pointer

IntelliSense: no suitable constructor exists to convert from “Entity *const” to “Entity”

What have I done incorrectly?

EDIT:

I have tried to use std::shared_ptr

std::list<std::shared_ptr<Entity>> m_entities;

I can’t add to the list this way though

Entity::Entity(Game *game) 
    : m_game(game)                  
{
    m_game->g_idGenerator->generateNewID();

    m_game->m_entities.push_back(this);      // ERROR
}

Using the following

m_game->m_entities.push_back(std::shared_ptr<Entity>(this));

gives me this error

error C2664: ‘void std::list<_Ty>::push_back(_Ty &&)’ : cannot convert parameter 1 from >’Entity’ to ‘std::tr1::shared_ptr<_Ty> &&’

EDIT 2:

Current code summary

for (entityIter i = m_entities.begin(); i != m_entities.end(); ++i)
{
    // *i dereferences the iterator and returns an Entity*
    // **i would additionally deference the pointer
    // Adding 'const' infront of Entity means that I can't alter the Entity
    Entity &e = **i;

    e.draw(dt);   // Causes access violation error with standard pointers
}   

Have tried converting to std:shared_ptr to see if it would avoid the error triggered by the code above.

However, I am now having trouble adding the Entity to the list of std::shared_ptr

    m_game->m_entities.push_back(std::shared_ptr<Entity>(this));

So in summary I have the access violation error with a standard pointer and I can’t add to the list with a shared_ptr.

Populating the list is done via the constructor of the base Entity class

Entity::Entity(Game *game) 
    : m_game(game)                  
{
    m_game->g_idGenerator->generateNewID();

            // shared_ptr version
    m_game->m_entities.push_back(std::shared_ptr<Entity>(this));  // ERROR C2664

            // raw pointer version
            //m_game->m_entities.push_back(this);  // ACCESS VIOLATION ERROR when calling methods
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T16:55:34+00:00Added an answer on June 11, 2026 at 4:55 pm
    const Entity &e = *i;
    

    *i dereferences the iterator and returns an Entity*.

    **i would additionally deference the pointer.

    To avoid keeping the reference around, you can use (*i)->memberFunction(...);

    Don’t forget that if you allocated the Entity’s with operator new that you also need to operator delete them.

    Here is an example using std::shared_ptr since your code is looking to complex to discuss on a page.

    I created a trivial Entity class and used it in std::shared_ptr. I put the same std::shared_ptr<Entity> multiple times purely for demonstration that std::shared_ptr manages this information including copying itself into the list.

    #include <memory>
    #include <iostream>
    #include <string>
    #include <list>
    using namespace std;
    
    class Entity {
    private:
      string name;
    public:
      Entity(const std::string& n) :
        name(n)
      { }
    
      const string& getName() {
        return name;
      }
    };
    
    int main(int argc, char** argv) {
      list<shared_ptr<Entity> > l;
    
      shared_ptr<Entity> sp(new Entity("Repeated!"));
    
      l.push_back(sp);
      l.push_back(sp);
      l.push_back(shared_ptr<Entity>(new Entity("Foo")));
      l.push_back(sp);
      l.push_back(shared_ptr<Entity>(new Entity("Bar")));
      l.push_back(sp);
    
      for(list<shared_ptr<Entity> >::const_iterator iter = l.begin();
          iter != l.end(); ++iter)
        {
          cout << ">> " << (*iter)->getName() << endl;
        }
    };
    

    Note: There is a difference between you putting the exact same raw pointer into multiple std::shared_ptr objects and copying a std::shared_ptr as push_back does. The latter case is fully managed by std::shared_ptr while the former case has each std::shared_ptr attempting to manage independently.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a design where I have a std::list of base pointers that I'd
I have a list of pointers to SomeClass objects in a Main class. Whilst
I have two class that inherit the same abstract base class: class base {
I have two classes like this (simplified and renamed): class Base { public: virtual
I have a list of pointers that reference time objects that need a turn
I have an STL list of pointers, along with another pointer of the same
I have a list of smart pointers. I want some of these smart pointers
I have a vector of Linked list pointers. Each LinkedList has a head pointer
if I have for example some class Base and derived from it Derived Also
I have a method void foo(list<shared_ptr<Base>>& myList); Which I'm trying to call with a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.