C++ : Pointing own class to NULL and IF-Check it.

Started by
4 comments, last by Trinavarta 17 years, 1 month ago
EDIT: Problem solved: See post #6 I am currently coding a simple rpg for coding experience. And now I ran into a problem and need some help/advice. Some Infos: Language: C++ API Used: Allegro Game Info: A very simple RPG - Small Tilebased Map -> Each tile may hold up to 3 Enemies. Hero walks around; and presses a button if he wants to attack a Monster on the Field he is currently standing. Classes used involved in this problem: Map Class -> Uses 5x5 array of the -> Tile Class -> Uses 3 variables from the -> Monster Class -> Saves the usual Monster-Stats (Level, Damage, HP etc.) ****************************** The Problem: I want the Monster to "despawn" if the Hero kills it and "respawn" (a new random-monster) some rounds later. So I had the Idea of just freeing the Monster-Variable-Memory and setting the pointer to NULL. Then I wrote a small IF(...) before drawing the Monster to check if its pointing to "NULL". At this point I noticed that this wont work. Because my compiler told me that he has no idea how to use "!=" or "==" together with my Monster-Class. I am really new to Pointers and Classes and overall dont have much coding experience. I could code a workaround and create an "I AM DEAD-Flag" or "I AM THE DEAD MONSTER DONT DRAW ME"-Variable. But this is not the right way, because I am sure I am doing here something terrible wrong and thats why i coded this... to learn something. :) Any advice/help/critique welcome! best regards, [Edited by - Trinavarta on March 21, 2007 3:52:52 PM]
Advertisement
Post the declaration of your tile class. If it looks something like the following, there shouldn't be a problem:

I'd write the class very differently than the below but i'm lazy and the naive approach is simple to illustrate:

class Tile{public:    void spawnMonster(int index);    void despawnMonster(int index);private:    //an array of 3 pointers to Monster    Monster *monsters[3];};void Tile::spawnMonster(int index){    assert( index >= 0 && index < 3 );    if ( monsters[index] == NULL )    {        monsters[index] = new Monster();    }}void despawnMonster(int index){    assert( index >= 0 && index < 3 );    delete monsters[index];    monsters[index] = NULL;}


-me
It sounds like you declared you monster variable like this:
Monster SomeName;

If that is the case then you can't compare it to NULL since SomeName isn't a pointer. SomeName can't become NULL so you don't have to test for it.

If you actually declared it:
Monster* SomeName;

Then something is wrong and you should show us:
- The declaration of the variable
- The if statement and the code around it
- The exact error
1) == and != should work perfectly fine between two pointers. If the compiler was complaining, you were likely trying to use == or != with an actual instance (not a pointer, for example a dereferenced pointer) of Monster. You can fix this by overloading operator== and operator!=, but I don't think that's actually the path you want to take.

2) If you have two pointers, freeing one and setting it to null will not in any way affect the other pointer, except to render it invalid (it will not become null, in other words). The following example
#include <iostream>int main(){  int *a = new int;  int *b = a;  delete a;  a = 0;  if(b == 0)    std::cout << "Hello!\n";}

will never print anything, because although we set a to 0 (null), b is unaffected. b still contains the address that a previously contained, except attempts to dereference b are now naughty.

It sounds like this might be what you're trying to do, and consequently it won't work.

A more robust solution is to actually remove the monster in question -- instead of a "I'm dead don't draw me" flag, simply remove it from the list of monsters that are going to be drawn, and release (delete) its memory.
Quote:Original post by Trinavarta
But this is not the right way, because I am sure I am doing here something terrible wrong and thats why i coded this... to learn something. :)

Good mindset you have there. Kudos.

--- ---Current Project: http://source.dev-null-productions.com/tw/"Perhaps the most fundamental problem, however, is that INTJs really want people to make sense."
Many thanks to everyone.

I think I have found it now with your help. (Just made a little workaround and seems to work now, have to change some more things in my mainloop now)


The Problem was that my Map Class did not return correctly:

DevCPP produced the following error message (Maybe someone got the same problem without finding anything useful with google/search; here are your Keywords and my Solution :) )

153 no match for 'operator!=' in 'maps::getField_1(int, int)((((player*)((singleplayer_game*)this)) + 12u)->player::getPosition_x(), (((player*)((singleplayer_game*)this)) + 12u)->player::getPosition_y()) == 0'


In the Tiles Class
...
monster* field_1;
...
field_1=new monster;
...

The Function
monster* tiles::getField_1()
{
return field_1;
};

--------

In the Map Class:
...
tiles map[5][5];
...
monster getField_1(int x, int y); (changed to monster* getField_1(int x, int y); )
...


monster maps::getField_1(int x, int y) (changed to monster* maps::getField_1(int x, int y) )
{
return *map[x][y].getField_1(); (changed to map[x][y].getField_1(); )
};

--------


And in my Mainloop:

if((map000.getField_1( player1.getPosition_x(), player1.getPosition_y() )) != NULL)
{
drawMonster_1();
}


********************************



I have cut out most lines for better reading. Hope I havent cut out something important...

Further comments to my funny looking code lines welcome :)

This topic is closed to new replies.

Advertisement