- Joined
- Nov 7, 2006
- Messages
- 2
- Reaction score
- 0
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(Inhabitant* anItem);
~Square();
Inhabitant* getInhabitant() const; /* This is what I need my main to call and get a pointer passed back */
void setInhabitant(Inhabitant* anItem);
void removeInhabitant();
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::getInhabitant() const
{
return item; /* This isn't working
*/
}
void Square::setInhabitant(Inhabitant* anItem)
{
item = anItem;
}
void Square::removeInhabitant()
{
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(Inhabitant* anItem);
~Square();
Inhabitant* getInhabitant() const; /* This is what I need my main to call and get a pointer passed back */
void setInhabitant(Inhabitant* anItem);
void removeInhabitant();
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::getInhabitant() const
{
return item; /* This isn't working
}
void Square::setInhabitant(Inhabitant* anItem)
{
item = anItem;
}
void Square::removeInhabitant()
{
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.