二进制表达式的操作数无效
invalid operands to binary expression?
我正在尝试实现使用继承/多态性的类,但我不明白为什么会出现这个错误:
二进制表达式错误的无效操作数('basic_ostream<char,std::_1::char_traits>'和'void')
错误在下面称为animate的函数中,它被两个**包围,就在main的正上方。由于某种原因,c->move
给出了此错误,但c->name
没有。我已经实现了其他一切,其他一切似乎都正常工作。这只是一个错误。有人能解释一下发生了什么事吗?基本上,这里的基类是生物,所有其他类都是从中派生的
#include <iostream>
#include <string>
using namespace std;
class Creature {
public:
Creature(string new_name) {
its_name=new_name;
}
virtual void move() const=0;
virtual bool isMortal() const =0;
string name() const {
return its_name;
}
virtual ~Creature();
private:
string its_name;
};
class Phoenix:public Creature {
public:
Phoenix(string new_name):Creature(new_name) {};
virtual void move() const{
cout<<"Squawkes, who is immortal, will now fly.";
}
virtual bool isMortal() const {
return false;
}
virtual ~Phoenix();
};
class Giant:public Creature {
public:
Giant(string new_name,int new_lbs): Creature(new_name) {
lbs=new_lbs;
}
virtual void move() const {
if (lbs<20) {
cout<<"Frawp, who is mortal, will now tromp.";
}
else {
cout<<"Gridwulfa, who is mortal, will now lumber.";
}
}
virtual bool isMortal() const {
return true;
}
virtual ~Giant();
private:
int lbs;
};
class Centaur:public Creature {
public:
Centaur(string new_name):Creature(new_name) {};
virtual void move() const{
cout<<"Squawkes, who is immortal, will now fly.";
}
virtual bool isMortal() const {
return false;
}
virtual~Centaur();
};
void animate(const Creature* c) //need to add & or is * sufficient?
{
cout << c->name() << ", who is ";
if (c->isMortal())
cout << "mortal";
else
cout << "immortal";
**cout << ", will now " << c->move() << ".n";**
}
Here is my main:
int main() {
Creature* creatures[4];
creatures[0] = new Phoenix("Squawkes");
// Giants have a name and a weight in tons. Giants under 20 tons
// walk by tromping; giants weighing at least 20 tons lumber.
creatures[1] = new Giant("Frawp", 17);
creatures[2] = new Giant("Gridwulfa", 22);
creatures[3] = new Centaur("Florence");
cout << "Here are the creatures." << endl;
for (int k = 0; k < 4; k++)
animate(creatures[k]);
// Clean up the creatures before exiting
cout << "Cleaning up." << endl;
for (int k = 0; k < 4; k++)
delete creatures[k];
}
我不应该改变主体或动画。我必须正确地实现所有的类
函数的返回类型为void
:
void Creature::move()
你不能向流发送void,你需要返回一些可打印的东西,string,int,bool,随便什么。。。
试试这个。。。。
#include <iostream>
#include <string>
using namespace std;
class Creature {
public:
Creature(string new_name) {
its_name=new_name;
}
virtual std::string move() const=0;
virtual bool isMortal() const =0;
string name() const {
return its_name;
}
virtual ~Creature();
private:
string its_name;
};
class Phoenix:public Creature {
public:
Phoenix(string new_name):Creature(new_name) {};
virtual std::string move() const{
return "fly";
}
virtual bool isMortal() const {
return false;
}
virtual ~Phoenix();
};
class Giant:public Creature {
public:
Giant(string new_name,int new_lbs): Creature(new_name) {
lbs=new_lbs;
}
virtual std::string move() const {
if (lbs<20)
return "tromp";
return "lumber";
}
virtual bool isMortal() const {
return true;
}
virtual ~Giant();
private:
int lbs;
};
class Centaur:public Creature {
public:
Centaur(string new_name):Creature(new_name) {};
virtual std::string move() const{
return "fly";
}
virtual bool isMortal() const {
return false;
}
virtual~Centaur();
};
void animate(const Creature* c) //need to add & or is * sufficient?
{
cout << c->name() << ", who is ";
if (c->isMortal())
cout << "mortal";
else
cout << "immortal";
**cout << ", will now " << c->move() << ".n";**
}
int main() {
Creature* creatures[4];
creatures[0] = new Phoenix("Squawkes");
// Giants have a name and a weight in tons. Giants under 20 tons
// walk by tromping; giants weighing at least 20 tons lumber.
creatures[1] = new Giant("Frawp", 17);
creatures[2] = new Giant("Gridwulfa", 22);
creatures[3] = new Centaur("Florence");
cout << "Here are the creatures." << endl;
for (int k = 0; k < 4; k++)
animate(creatures[k]);
// Clean up the creatures before exiting
cout << "Cleaning up." << endl;
for (int k = 0; k < 4; k++)
delete creatures[k];
}
相关文章:
- 类型"int"和"const char [15]"到二进制"运算符<<"的无效操作数^
- 二进制表达式 cout 问题的无效操作数
- 二进制'operator+' 'int*'和'int*'类型的无效操作数
- 'const char [26]' 和 'LPSTR ' 类型的无效操作数
- 如何修复"二进制表达式的无效操作数"?
- 如何将无效操作数修复为二进制表达式
- 错误:二进制表达式('float' 和 'float')返回的无效操作数 (x & (1 << 31)) == 0
- 加密注释C++具有无效操作数的编译错误
- 如何将无效操作数修复为带有字符串的二进制表达式?
- 模板 &&运算符中的无效操作数+
- 类型别名和运算符<<使用 ostream_iterator 重载会导致无效操作数
- 类型为"double" snd const char [3]' 到二进制'operator<<'的无效操作数
- 二进制表达式的语义问题无效操作数("双精度"和"双精度")
- 'double' 和 'const char [2]' 到二进制"运算符!="的无效操作数|
- 具有重载运算符<<的无效操作数
- 'double' 和 'double' 类型的无效操作数
- 整数和无效操作数中的小数*已更新
- 二进制'operator<<' 'const char [6]'和'const char [3]'类型的无效操作数
- 将算法c++的无效操作数替换为二进制表达式
- 'char*'和'const char [2]'到二进制'operator+'类型的 C++ 无效操作数