我不确定是什么问题。它给我错误:
error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'void' (or there are no acceptable conversion)
我重载了 << 所以它不应该给我这个错误,对吧?
#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;
指定返回类型为 void
的函数.这意味着它不会返回任何东西。当您定义 Animal::move
时也会发生同样的情况作为virtual void move() = 0;
.
out << animal.getAnimalType() << animal.talk() << ", " << animal.move();
尝试打印 animal.talk()
的结果和 animal.move()
的结果- 两者都不存在(记住,talk()
和 move()
都不返回任何值!)
我是一名优秀的程序员,十分优秀!