Returning a pointer from a class.

Joined
Nov 8, 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.
 
Joined
Nov 8, 2006
Messages
2
Reaction score
0
Not sure if it helps but...

Well I'm not sure if this will help but when I ran the program on a unix system I got "Segmentation Fault (core dumped)" as my error
 
Joined
Nov 9, 2006
Messages
3
Reaction score
0
Provide Assignment op in ur class

samthesadpanda said:
Well I'm not sure if this will help but when I ran the program on a unix system I got "Segmentation Fault (core dumped)" as my error
Provide Assignment op in ur class
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top