Hey, I'm having some trouble with a program I'm trying to write and was hoping someone here could help me. Basically I have a dynamic 2D array of the 'Square' class and in the square class is the pointer 'item.' 'item' is a pointer of another class that I have called 'Inhabitant' and I have 3 derived classes off of 'Inhabitant.' I need to somehow have a pointer function in 'square' that returns the pointer 'item.'
From the header file for the square class:
class Square
{
public:
Square();
Square(Inhabita nt* anItem);
~Square();
Inhabitant* getInhabitant() const; /* This is what I need my main to call and get a pointer passed back */
void setInhabitant(I nhabitant* anItem);
void removeInhabitan t();
private:
Inhabitant* item; /* This is the pointer that I need to have returned */
};
From the cpp file:
Square::Square( )
{
item = NULL;
}
Square::Square( Inhabitant* anItem)
{
item = anItem;
}
Square::~Square ()
{
delete item;
}
Inhabitant* Square::getInha bitant() const
{
return item; /* This isn't working */
}
void Square::setInha bitant(Inhabita nt* anItem)
{
item = anItem;
}
void Square::removeI nhabitant()
{
item = NULL;
}
I don't get an error while compiling but rather when running the program I get:
Unhandled exception at 0x00419e66 in game.exe: 0xC0000005: Access violation reading location 0xfdfdfdfd.
If anyone could help it would be greatly appreciated. Please note that I am being forced to have the program set up this way so I can't use vectors instead of my dynamic 2D array ect.
From the header file for the square class:
class Square
{
public:
Square();
Square(Inhabita nt* anItem);
~Square();
Inhabitant* getInhabitant() const; /* This is what I need my main to call and get a pointer passed back */
void setInhabitant(I nhabitant* anItem);
void removeInhabitan t();
private:
Inhabitant* item; /* This is the pointer that I need to have returned */
};
From the cpp file:
Square::Square( )
{
item = NULL;
}
Square::Square( Inhabitant* anItem)
{
item = anItem;
}
Square::~Square ()
{
delete item;
}
Inhabitant* Square::getInha bitant() const
{
return item; /* This isn't working */
}
void Square::setInha bitant(Inhabita nt* anItem)
{
item = anItem;
}
void Square::removeI nhabitant()
{
item = NULL;
}
I don't get an error while compiling but rather when running the program I get:
Unhandled exception at 0x00419e66 in game.exe: 0xC0000005: Access violation reading location 0xfdfdfdfd.
If anyone could help it would be greatly appreciated. Please note that I am being forced to have the program set up this way so I can't use vectors instead of my dynamic 2D array ect.
Comment