循环遍历指针列表时的问题 - C++

use*_*893 0 c++ pointers loops

我有一个指向基本抽象类(实体)的指针列表

std::list<Entity*> m_entities;
Run Code Online (Sandbox Code Playgroud)

我已经创建了一个用于迭代这个类的typedef

typedef std::list<Entity*>::const_iterator entityIter;
Run Code Online (Sandbox Code Playgroud)

然后我尝试迭代列表中的每个指针

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

尝试引用每个指针时出现以下错误

IntelliSense:没有合适的构造函数可以从"Entity*const"转换为"Entity"

我做错了什么?

编辑:

我试过使用std :: shared_ptr

std::list<std::shared_ptr<Entity>> m_entities;
Run Code Online (Sandbox Code Playgroud)

我不能这样添加到列表中

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

    m_game->m_entities.push_back(this);      // ERROR
}
Run Code Online (Sandbox Code Playgroud)

使用以下内容

m_game->m_entities.push_back(std::shared_ptr<Entity>(this));
Run Code Online (Sandbox Code Playgroud)

给了我这个错误

错误C2664:'void std :: list <_Ty> :: push_back(_Ty &&)':无法将参数1从>'Entity'转换为'std :: tr1 :: shared_ptr <_Ty> &&'

编辑2:

当前代码摘要

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
}   
Run Code Online (Sandbox Code Playgroud)

尝试过转换为std:shared_ptr以查看是否可以避免上述代码触发的错误.

但是,我现在无法将实体添加到std :: shared_ptr列表中

    m_game->m_entities.push_back(std::shared_ptr<Entity>(this));
Run Code Online (Sandbox Code Playgroud)

总而言之,我有一个标准指针的访问冲突错误,我无法添加到shared_ptr列表.

填充列表是通过基本Entity类的构造函数完成的

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
}
Run Code Online (Sandbox Code Playgroud)

Jon*_*eng 13

const Entity &e = *i;
Run Code Online (Sandbox Code Playgroud)

*i 取消引用迭代器并返回实体*.

**i 还会尊重指针.

为避免保留参考,您可以使用 (*i)->memberFunction(...);

不要忘记,如果你分配了实体operator new,你也需要operator delete它们.

下面是一个使用示例,std::shared_ptr因为您的代码看起来很复杂,无法在页面上进行讨论.

我创建了一个简单的Entity类并使用它std::shared_ptr.我把它std::shared_ptr<Entity>多次用于std::shared_ptr管理这些信息的演示,包括将自己复制到列表中.

#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;
    }
};
Run Code Online (Sandbox Code Playgroud)

注意:有你把完全相同的原始指针到多个之间的差异std::shared_ptr对象和复制一个std::shared_ptrpush_back呢.后一种情况完全由std::shared_ptr前一种情况完全管理,而前一种情况各自std::shared_ptr试图独立管理.