我一直在网上阅读了很多关于这个错误的信息,为什么会出现这种错误,但是我在代码中找不到错误。C++读取字符串的错误
我有一个Inventory
类继承的名单GameObject
指针:
#ifndef INVENTORY_H
#define INVENTORY_H
#include "GameObject.h"
#include <list>
template <class GameObject>
class Inventory : public std::list<GameObject*>
{
private:
public:
Inventory() : std::list<GameObject*>::list() {}
};
#endif
GameObject
类看起来是这样的:
class GameObject : public Updateable
{
private:
...
Inventory<GameObject*> m_inventory;
public:
...
void SetInventory(Inventory<GameObject*> inventory);
Inventory<GameObject*>& GetInventory();
};
然后,我通过这个方法填充新Inventory
对象:
Inventory<GameObject*>& GameInitializer::ConfigureItems(XMLElement* xmlGameObject) {
Inventory<GameObject*>* inv = new Inventory<GameObject*>();
...
while (currElement != NULL) {
GameObject* item = new GameObject();
// Configure all properties of the item
item->SetId(currElement->Attribute("id"));
item->SetPropertyHolder(ConfigureProperties(currElement));
item->SetName(item->GetPropertyHolder().GetProperty("NAME").As<string>());
// Add item to inventory
(*inv).push_back(&item);
currElement = currElement->NextSiblingElement();
}
return (*inv);
}
但是whe从来没有这种Inventory
对象的引用返回,在GameObject
类的成员变量(id
,name
)无法从内存中读取:
偏离主题,但可能有用:[关于从STL容器继承](http://stackoverflow.com/questions/2034916/is-it-okay-to-inherit-implementation-from-stl-containers-而不是委托) – emlai 2015-02-08 15:25:44
此代码将从[智能指针]的使用中受益匪浅(http://stackoverflow.com/questions/106508/what-is-a-smart-pointer-and-when-should-i -use-one) – Edward 2015-02-08 15:32:28
只是为了确认,你是否知道'm_inventory'结束了从'std :: list'的继承?我认为'*'就足够了,但我不确定你的代码是否需要两个。 –
emlai
2015-02-08 15:37:23