I'm not sure what the problem is. It gives me the error:
error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'void' (or there is no acceptable conversion)
I overloaded the << so it shouldn't be giving me this error, right?
#ifndef ANIMAL_H_
#define ANIMAL_H_
#include <iostream>
#include <string>
using namespace std;
static int counter;
static int getAnimalCount() { return counter; }
class Animal {
protected:
string *animalType;
public:
virtual void talk() = 0;
virtual void move() = 0;
string getAnimalType() { return *animalType; }
//PROBLEM RIGHT HERE V
friend ostream& operator<<(ostream&out, Animal& animal) {
return out << animal.getAnimalType() << animal.talk() << ", " << animal.move();
};
~Animal() {
counter--;
animalType = NULL;
}
};
class Reptile : public Animal {
public:
Reptile() { animalType = new string("reptile"); };
};
class Bird : public Animal {
public:
Bird() { animalType = new string("bird"); };
};
class Mammal : public Animal{
public:
Mammal() { animalType = new string("mammal"); };
};
#endif /* ANIMAL_H_ */
virtual void talk() = 0;
specifies a function with return type void
. This means it does not return anything. The same happens when you define Animal::move
as virtual void move() = 0;
.
out << animal.getAnimalType() << animal.talk() << ", " << animal.move();
tries to print the result of animal.talk()
and the result of animal.move()
- neither of which exists (remember, neither talk()
nor move()
return any value!)