0

My Game.cpp function init throws run time error while adding objects to a list. I have the console print message before and after the place of error for debugging. main.cpp creates Game object and calls function init() which creates objects and a list. The function should add them to the list. There are no compile errors and I had this working on what seems identical project. We should expect the message "Items Start Adding" to print before the adding to list and "Items Added" to print afterwards. This would let us know they have been added. I have added the error image below that I get and it seems to open up the list class and point to a exact line of issue in the list class

Thanks

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);
}
Daniel
  • 13
  • 1
  • 4
  • Is the code you tested the same **exactly** as what you posted now? – Mat Nov 04 '16 at 14:52
  • I have minimized it for the question – Daniel Nov 04 '16 at 14:53
  • You need move player and enemies initialisation to the constuctor or to the Init. The class definition is not correct. – Alex Nov 04 '16 at 14:55
  • @Alex: that's legal in C++ >= 11. Daniel, did you verify that the code you posted still has the error. Seems to work fine. – Mat Nov 04 '16 at 14:57
  • There are other functions in the Game class that need to use the object so can't put in init. I tried it anyways and same issue. I tried adding them to constructor, also failed as init couldn't access them. I have exactly this set up , working on another identical program with more complexity. I simply was trying to make a striped down version and it stopped working – Daniel Nov 04 '16 at 15:00
  • Yes wont work for me. Let me try it on brand new project. Ill copy and paste it into new project – Daniel Nov 04 '16 at 15:01
  • @Mat: Interesting, so it appears to be the same as putting those in constructor. Thanks for the info. – Alex Nov 04 '16 at 15:05
  • Ok seems to be strange. Im going to add the rest of code for init, because it is the problem. Like i assumed because "Items Added" didnt print, that was the problem before, but it seems its a iter loop after is causing it – Daniel Nov 04 '16 at 15:08
  • The error was in the for loops with the number 23 , should be 22 – Daniel Nov 04 '16 at 15:12
  • 1
    Any particular reason you are using a list of pointers instead of a list of objects? or why you are using `list` instead of `vector`? – UnholySheep Nov 04 '16 at 15:15
  • Our lecturer says we have to simple put – Daniel Nov 04 '16 at 15:39

0 Answers0