Returning a pointer from a class.

Discussion in 'C Programming' started by samthesadpanda, Nov 8, 2006.

  1. samthesadpanda

    samthesadpanda

    Joined:
    Nov 8, 2006
    Messages:
    2
    Location:
    Australia
    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.
    samthesadpanda, Nov 8, 2006
    #1
    1. Advertising

  2. samthesadpanda

    samthesadpanda

    Joined:
    Nov 8, 2006
    Messages:
    2
    Location:
    Australia
    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
    samthesadpanda, Nov 8, 2006
    #2
    1. Advertising

  3. samthesadpanda

    PraveenAyinikkal

    Joined:
    Nov 9, 2006
    Messages:
    3
    Provide Assignment op in ur class

    Provide Assignment op in ur class
    PraveenAyinikkal, Nov 9, 2006
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. tirath
    Replies:
    3
    Views:
    695
    Ivan Vecerina
    Oct 12, 2003
  2. masood.iqbal@lycos.com

    Pointer-to-pointer-to-pointer question

    masood.iqbal@lycos.com, Feb 3, 2005, in forum: C Programming
    Replies:
    10
    Views:
    669
    Chris Torek
    Feb 4, 2005
  3. Jahagirdar Vijayvithal S
    Replies:
    2
    Views:
    425
    Jahagirdar Vijayvithal S
    Aug 7, 2005
  4. Alberto Luaces
    Replies:
    6
    Views:
    375
    Fraser Ross
    Jun 19, 2006
  5. subramanian100in@yahoo.com, India

    converting derived class pointer to private base class pointer

    subramanian100in@yahoo.com, India, Aug 14, 2010, in forum: C++
    Replies:
    8
    Views:
    932
    gwowen
    Aug 18, 2010
Loading...

Share This Page