所以这是我的 main / Initializer 函数的简化版本。当我调用它并且它必须向玩家发明者添加任何项目时,我得到一个 Debug Assertation Failed 错误。
在我看来,我有点混淆了范围?
我是否在函数范围内声明了一些新内容,然后无法在 main 中再次访问它?
我在函数内部尝试了一些东西,比如使用 Getters/Setters 而不是完全分配,就像p_player = p
但我认为这根本不能解决问题,我有点困惑。
int main()
{
Array<Item> items(3);
string itemsfilename = "itemsfile.txt";
Initializer::InitializeItems(items, itemsfilename);
Login login;
Player p1;
string filename = login.LoginToGame();
Initializer::InitializePlayer(p1, rooms, items, 3, filename);
}
void Initializer::InitializePlayer(Player& p_player, HashTable<string, Room>& p_rooms, Array<Item>& p_items, int p_numItems, std::string& p_filename)
{
ifstream playerfile(p_filename);
int inventorycount = 0;
//all the stuff needed to make a player
std::string name;
int health;
int confidence;
int humor;
int speed;
std::string room;
Room* currentRoom;
Inventory inventory(100);
//reading in values from file
for(int i = 0; i < inventorycount; i++)
{
playerfile.getline(value, 256);
std::string item(value);
for(int j = 0; j < p_numItems; j++)
{
if(p_items[j].GetName() == item)
{
inventory.AddItem(&(p_items[j])); //This line taken out, removes the error.
}
}
}
Player p(name, health, confidence, humor, speed, currentRoom, inventory);
p_player = p;
}
AddItem() 获取一个指向项目的指针,然后将其附加到它的 DLinkedList。
编辑:
我得到的错误是
调试断言失败!
程序:zzz
文件 f:\dd/vctools/crt_bld/self_x86/crt/src/dbgdel.cpp 行:52
表达式:_Block_TYPE_IS_VALID(pHead->nBlockUse)
AddItem() 代码:
bool AddItem(Item* p_item)
{
if(p_item->GetWeight() + m_weight <= m_maxWeight)
{
m_inventory.Append(p_item);
m_weight += p_item->GetWeight();
}
else
{
return false;
}
return true;
}