C ++将对象添加到列表运行时错误

时间:2016-11-04 14:46:22

标签: c++ list

我的 Game.cpp 函数init在向列表添加对象时抛出运行时错误。我在调试错误之前和之后都有控制台打印消息。 main.cpp 创建Game对象并调用函数init()来创建对象和列表。该函数应将它们添加到列表中。没有编译错误,我有这个工作似乎相同的项目。我们应该期望在添加到列表之前打印“Items Start Adding”消息,然后打印“Items Added”以进行打印。这会让我们知道他们已被添加。我在下面添加了错误图像,它似乎打开了列表类并指向列表类中的确切问题

由于

Error Image here

的main.cpp

//Included libraries
#include "GameObject.h"
#include "Enemy.h"
#include "Player.h"
#include "Game.h"
#include <iostream>
#include <string>

using namespace std;

int main()
{
    Game game;
    game.init();
}

Game.h

#ifndef GAME_H
#define GAME_H
#include "GameObject.h"
#include "Player.h"
#include "Enemy.h"
#include <list>
#include <time.h>
#include <array>
#include <string>
#include <iostream>
using namespace std;

class Game
{
public:
    Game();
    ~Game();
    void init();

private:

    //Game Objects
    GameObject* player1 = new Player();
    GameObject* enemy1 = new Enemy();
    GameObject* enemy2 = new Enemy();
    GameObject* enemy3 = new Enemy();
    GameObject* enemy4 = new Enemy();

    //List and iterator
    list <GameObject*> listOfObjects;

};
#endif

Game.cpp

#include "Game.h"

Game::Game(){}
Game::~Game(){}
void Game::init()
{
    cout << "Items Start Adding" << endl;//Does print

    //Add objects to list ERROR ERROR HERE 
    listOfObjects.push_back(player1);
    listOfObjects.push_back(enemy1);
    listOfObjects.push_back(enemy2);
    listOfObjects.push_back(enemy3);
    listOfObjects.push_back(enemy4);

    cout << "Items Added" << endl;//Does not print

//Give each item in list random unique place in map
for (iter = listOfObjects.begin(); iter != listOfObjects.end(); iter++)
{
    do
    {
        x = (rand() % 22)+1;
        y = (rand() % 22)+1;
    }while (array[y][x] == 1);


    (*iter)->spawn(to_string(ID), 160, 1, y, x);
    ID = ID + 1;
}

cout << "Init Function Complete" << endl;
Sleep(5000);
}

0 个答案:

没有答案