繁体   English   中英

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

[英]invalid operands to binary expression?

我正在尝试实现使用继承/多态性的类,但我无法弄清楚为什么我将这个无效的操作数转换为二进制表达式错误(“basic ostream char std 1 char traits char 和 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()

你不能向流发送一个空值,你需要返回一些可打印的东西,字符串,整数,布尔,不管......

尝试这个....

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


}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM