二进制表达式的无效操作数?

Posted

技术标签:

【中文标题】二进制表达式的无效操作数?【英文标题】:invalid operands to binary expression? 【发布时间】:2016-04-30 23:45:01 【问题描述】:

我正在尝试实现使用继承/多态性的类,但我不知道为什么会出现此错误:

二进制表达式错误的无效操作数('basic_ostream' 和 '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];



我不应该更改主要或动画。我必须正确实现所有类

【问题讨论】:

错误信息说明出了什么问题。你不能 &lt;&lt; 一个带有 void 的流。 move 应该返回一个字符串;单个动词(“lumber”、“fly”、“tromp”)。您已经硬编码了 animate 应该在 move 中构建的字符串。 这能回答你的问题吗? How do I get my method to recognize my object value in c++ 【参考方案1】:

你的函数的返回类型是void

void Creature::move()

您不能将 void 发送到流中,您需要返回可打印的内容、字符串、int、bool 等等...

【讨论】:

【参考方案2】:

试试这个....

#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];



【讨论】:

以上是关于二进制表达式的无效操作数?的主要内容,如果未能解决你的问题,请参考以下文章

错误:二进制表达式的操作数无效('float' 和 'float')

使用Boost.Geometry时“二进制表达式的无效操作数”?

将元素推入优先级队列时,二进制表达式的操作数无效

与维吉尼斗争! (在 C 中)二进制表达式('int *' 和 'int')和其他东西的无效操作数

如何解决此错误:二进制表达式的操作数无效('std::vector<double>' 和 'double')?

C ++新手:从实现学习单独的接口并得到错误:VSCode中的二进制表达式的操作数无效