Thread: deleting and re-assigning a global variable pointer

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    52

    deleting and re-assigning a global variable pointer

    Hi,

    I have a global variable
    Game * game;

    in main I have game = new Game();

    and I have another function which monitors the progress of the game and if the current level is completed then I want to do game = new Game() again to start a new game but I want to clean up the heap so I do the following:

    Code:
    void Timer(int extra)
    {
    	...
    	game->detectColisions();
    	if (game->detectLevelCompleted())
    	{
    		if (game->level < 50)
    		{
    			delete game;
    			game = new game();
    		}
    		else
    			cout << "you beat the game congrats" << endl;
    		//display menu
    	}
    	...
    	glutPostRedisplay();
    	glutTimerFunc(30,Timer,0);
    }
    but it won't compile. I'm using VC++ and I get the following compiler error:
    error C2061: syntax error : identifier 'game'

    Is this because I'm using a global variable?
    I did something similar to pointers to characters instead of game but the pointer was a member and not global. Can Game * game not be a global variable if I want to delete it and reassign it as I showed above?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    game = new Game();
    game = new game();
    Compare and contrast.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    52
    Damn! I noticed that just before you posted and was hoping I could change it in time! How embarassing lol sorry

Popular pages Recent additions subscribe to a feed