我不确定是什么问题。它给了我错误:错误C2679:二进制'我重载了#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_ */ (adsbygoogle = window.adsbygoogle || []).push({}); 最佳答案 virtual void talk() = 0;指定返回类型为void的函数。这意味着它不返回任何东西。当您将Animal::move定义为virtual void move() = 0;时,也会发生相同的情况。out << animal.getAnimalType() << animal.talk() << ", " << animal.move();尝试打印animal.talk()的结果和animal.move()的结果-都不存在(请记住,talk()和move()都不返回任何值!) (adsbygoogle = window.adsbygoogle || []).push({});
09-06 18:35