I guess this is a really simple question and, probably, one that has been answered several times over. However, I really do suck at C++ and have searched to no avail for a solution. I would really appreciate the help.
Basically:
#ifndef ANIMAL_H
#define ANIMAL_H
class Animal
{
public:
void execute();
void setName(char*);
Animal();
virtual ~Animal();
private:
void eat();
virtual void sleep() = 0;
protected:
char* name;
};
class Lion: public Animal
{
public:
Lion();
private:
virtual void sleep();
};
class Pig: public Animal
{
public:
Pig();
private:
virtual void sleep();
};
class Cow: public Animal
{
public:
Cow();
private:
virtual void sleep();
};
#endif
Is the header file, where:
#include <iostream>
#include "Animal.h"
using namespace std;
Animal::Animal()
{
name = new char[20];
}
Animal::~Animal()
{
delete [] name;
}
void setName( char* _name )
{
name = _name;
}
void Animal::eat()
{
cout << name << ": eats food" << endl;
}
void Animal::execute()
{
eat();
sleep();
}
Lion::Lion()
{
name = new char[20];
}
void Lion::sleep()
{
cout << "Lion: sleeps tonight" << endl;
}
Pig::Pig()
{
name = new char[20];
}
void Pig::sleep()
{
cout << "Pig: sleeps anytime, anywhere" << endl;
}
Cow::Cow()
{
name = new char[20];
}
void Cow::sleep()
{
cout << "Cow: sleeps when not eating" << endl;
}
is the C file. As you can see, really simple stuff, but, I get the: "error: ‘name’ was not declared in this scope" whenever I try to compile.
It compiles if I comment out the setName method. Iv tried setting 'name' to public and still get the same error. I have also tried using "this->name = _name" in setName(), which results in "invalid use of ‘this’ in non-member function".
I don't know what else to search for. Thanks in advance.