C ++错误读取字符串的字符
C++ Error reading characters of string
我在 Internet 上阅读了很多有关此错误的信息以及为什么会导致此错误,但我找不到我的代码中的错误。
我有一个 Inventory
class 继承了指向 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 看起来像这样:
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);
}
但是每当返回这个Inventory
对象的引用时,GameObject
class(id
,name
)中的成员变量就无法从内存中读取:
在您的第二个代码块中,您 push_back()
一个指向局部变量的指针(即 GameObject* item
)。它在返回时被销毁并使 IDE 指出这个错误。
我建议更改此设置:
Inventory<GameObject*> m_inventory;
对此:
Inventory<GameObject> m_inventory;
所以它将是 std::list<GameObject*>
而不是 std::list<GameObject**>
。
存储指针到指针到 GameObject
元素似乎是多余的,简单地存储指向 GameObject
的指针应该就足够了,并使您的其他代码更简单(例如这一行:(*inv).push_back(&item)
).
我最近遇到了这个问题,这是因为我在函数顶部声明变量然后再次声明它。
我在 Internet 上阅读了很多有关此错误的信息以及为什么会导致此错误,但我找不到我的代码中的错误。
我有一个 Inventory
class 继承了指向 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 看起来像这样:
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);
}
但是每当返回这个Inventory
对象的引用时,GameObject
class(id
,name
)中的成员变量就无法从内存中读取:
在您的第二个代码块中,您 push_back()
一个指向局部变量的指针(即 GameObject* item
)。它在返回时被销毁并使 IDE 指出这个错误。
我建议更改此设置:
Inventory<GameObject*> m_inventory;
对此:
Inventory<GameObject> m_inventory;
所以它将是 std::list<GameObject*>
而不是 std::list<GameObject**>
。
存储指针到指针到 GameObject
元素似乎是多余的,简单地存储指向 GameObject
的指针应该就足够了,并使您的其他代码更简单(例如这一行:(*inv).push_back(&item)
).
我最近遇到了这个问题,这是因为我在函数顶部声明变量然后再次声明它。