Start New Discussion within our Software Development Community

//this is the class that I must declare to be public

class Zoo
{
private:
//create 4 pointers to instances of house class
Animal *M;
Animal *C;
Animal *D;
Animal *L;


};
// constructor

Zoo::Zoo()
{
Animal *M;= new Animal();
char M[64];
strcpy(M, "Monkey");
M->SetHouseName(M);
Animal *C; = new Animal();
char R[64];
strcpy(C, "Cat");
C->SetHouseName(C);
Animal *D;= new Animal();
char D[64];
strcpy(D, "Dog");
D->SetHouseName(D);
Animal *L; = new Animal();
char L[64];
strcpy(L, "Lion");
L->SetHouseName(L);
}

For the constructor I recieve 4 errors stating redefinition; different types of indirection. I don't understand why I am recieving it and for the deconstrutor I have: which I highly expect for it to be wrong. Feedback would be great!

Animal ::~Animal ()
{
delete *M, *C, *D, *L;
}

Use [[i][/i]code[i][/i]] tags please.

First, watch your syntax. Any time you have a ; you are terminating a statement. So Animal *M;= new Animal(); is two separate statements: Animal *M; and = new Animal(); .

The first 'redeclares' the variable M. The second is error. What you want to say is M = new Animal; Secondly, watch your delete statement. Use one per object. delete M; delete C; etc.

Lastly, don't forget to declare your constructor and destructor. Your compiler may let you get away with just defining them, but that's not standard C++.

class Zoo {
  ...
  public:
    Zoo();
   ~Zoo();
  };

Zoo::Zoo() {
  M = new Animal;
  }

Zoo::~Zoo() {
  delete M;
  }

Hope this helps.

This article has been dead for over six months. Start a new discussion instead.