当我尝试在C ++中调用继承的函数时发生错误

时间:2019-11-18 04:57:05

标签: c++ inheritance

我有3个类,GameObject,从GameObject继承的Building和从PokemonCenter继承的PokemonCenter。当我尝试在PokemonCenter中调用Building :: ShowStatus()函数时,始终收到“对二进制表达式无效的操作数”错误。

Building.h

#ifndef BUILDING_H
#define BUILDING_H
#include "Point2D.h"
#include "GameObject.h"

class Building : public GameObject
{

private:
    unsigned int pokemon_count;

public:
    Building();
    Building(char,int, Point2D);
    void AddOnePokemon();
    void RemoveOnePokemon();
    void ShowStatus();
    bool ShouldBeVisible();

};

#endif

Building.cpp

#include "Building.h"
#include "GameObject.h"
#include <iostream>
using namespace std;

Building::Building()
{
    display_code = 'B';
    location;
    id_num = ' ';
    state = '0';
    pokemon_count = 0;
    cout << "Building default constructed";
}

Building::Building(char in_code,int in_id, Point2D in_loc)
{
    id_num = in_id;
    location = in_loc;
    display_code = in_code;
    state = '0';
    pokemon_count = 0;
    cout << "Building constructed";
}

void Building::ShowStatus()
{
    cout << "\"(" << pokemon_count << "\"pokemon is/are in this building";
}

PokemonCenter.h

#ifndef POKEMONCENTER_H
#define POKEMONCENTER_H
#include "Point2D.h"
#include "Building.h"

class PokemonCenter: public Building
{
private:
    unsigned int stamina_capacity;
    unsigned int num_stamina_points_remaining;
    double dollar_cost_per_stamina_point;
    PokemonCenter();
    PokemonCenter(int,double,unsigned int, Point2D);

public:
    bool HasStaminaPoints();
    unsigned int GetNumStaminaPointsRemaining();
    bool CanAffordStaminaPoints(unsigned int, Point2D);
    double GetDollarCost(unsigned int);
    unsigned int DistributeStamina(unsigned int);
    bool Update();
    void ShowStatus();
};


enum PokemonCenterStates
{
    STAMINA_POINTS_AVAILABLE = 0,
    NO_STAMINA_POINTS_AVAILABLE = 1
};


#endif

PokemonCenter.cpp

#include "PokemonCenter.h"
#include "Point2D.h"
#include "Building.h"
#include "GameObject.h"
#include <iostream>
using namespace std;

PokemonCenter::PokemonCenter()
{
    id_num = ' ';
    location;
    display_code = 'C';
    stamina_capacity = 100;
    num_stamina_points_remaining = stamina_capacity;
    dollar_cost_per_stamina_point = 5;
    state = STAMINA_POINTS_AVAILABLE;
    cout << "PokemonCenter default constructed";
}

PokemonCenter::PokemonCenter(int in_id, double stamina_cost, unsigned int stamina_cap, Point2D in_loc)
{
    id_num = in_id;
    location = in_loc;
    dollar_cost_per_stamina_point = stamina_cost;
    stamina_capacity = stamina_cap;
    num_stamina_points_remaining = stamina_capacity;
    state = STAMINA_POINTS_AVAILABLE;
    cout << "PokemonCenter constructed";

}

void PokemonCenter::ShowStatus()
{
cout << "Pokemon Center Status: " << Building::ShowStatus() << endl;
}

1 个答案:

答案 0 :(得分:0)

问题是Building::ShowStatus()的返回类型为void。在operator<<std::ostream之间未定义void。因此,您不能使用

cout << "Pokemon Center Status: " << Building::ShowStatus() << endl;

将其更改为:

cout << "Pokemon Center Status: ";
Building::ShowStatus();
cout << endl;