gpt4 book ai didi

c++ - 错误 C2679 : binary '<<' : no operator found which takes a right-hand operand of type 'void' (or there is no acceptable conversion)

转载 作者:太空宇宙 更新时间:2023-11-04 15:40:59 26 4
gpt4 key购买 nike

我不确定是什么问题。它给我错误:

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() 都不返回任何值!)

关于c++ - 错误 C2679 : binary '<<' : no operator found which takes a right-hand operand of type 'void' (or there is no acceptable conversion),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23231165/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com