问题的结构是这样的 Food 是一个抽象基类;Plant 和 Animal 直接继承自它。草食动物、肉食动物和杂食动物继承自动物,而水果、坚果和树叶继承自植物狐猴、考拉和松鼠继承草食动物
总的来说,这是一团糟,但对于锻炼来说是必要的。整个项目在 GitHub 上可用 https://github.com/joekitch/OOP_JK_Assignment_4/blob/master/OOP_JK_Assignment_4/Lemur.h 完整的类图也在 GitHub 上
但这里有一些相关的东西(至少,我认为是相关的)首先是食物类,它几乎什么都没有
#pragma once
#include <string>
#include <list>
using namespace std;
class Food
{
public:
Food(){ }
virtual ~Food(){ }
};
接下来是Animal,它包含了hunt()和eat()函数的虚函数
#pragma once
#include "Food.h"
#include "Animal.h"
#include "Plant.h"
#include <iostream>
#include <string>
#include <list>
using namespace std;
class Animal : public Food
{
public:
Animal(void) : name(), alive(true), age(0), calories(0), weight(0) { }
Animal(string& animal_name, int animal_age, int animal_calories, double animal_weight) :
name(animal_name), alive(true), age(animal_age), calories(animal_calories), weight(animal_weight), maxcalories(animal_calories) {}
virtual ~Animal(){}
virtual bool eat(Food* food){return false;};
virtual bool hunt(list<Food*> &foodlist){return false;};
virtual void PrintSelf(){};
virtual string& getName(){
return name;
};
std::string name;
bool alive;
int age, calories, maxcalories;
double weight;
};
这是 Herbivore,它完全定义了 hunt() 函数,这就是问题开始的地方(请参阅我在 hunt() 中的评论)。Hunt 接收一个 Food* 类型的列表,该列表在 main() 中全局声明
#pragma once
#include "Animal.h"
//#include "Lemur.h"
#include "Plant.h"
#include "Fruit.h"
#include "Leaf.h"
#include "Nut.h"
#include <iostream>
#include <string>
#include <list>
#include <typeinfo>
using namespace std;
class Herbivore : public virtual Animal
{
public:
Herbivore() {}
virtual ~Herbivore(){}
virtual bool eat(Food* food) {cout << "herbivore.h eat() called" << endl; return true;};
bool hunt(list<Food*> &foodlist) //herbivore version of hunt()
{
int fruitcounter=0;
int plantcounter=0;
string name;
for (list<Food*>::iterator it = foodlist.begin(); it != foodlist.end(); it++)
{
if (Plant* temp = dynamic_cast<Plant*>(*it))
{
//this is there the problems start. the above dynamic cast SHOULD make temp
//non-null if the thing i'm looking at is a child of plant (that is, if the thing
//in the food list is a fruit or a nut or a leaf). And indeed it does...but the
//next dynamic cast (in the eat() function of Lemur) doesn't detect any fruits....
plantcounter++;
if ( eat(*it) )
fruitcounter++;
//return true;
}
}
cout << "there are " << fruitcounter << " fruits and " << plantcounter << " plants in the list." << endl;
return false;
};
};
这是水果类。没什么特别值得注意的,我只是把它放在这里以防万一它们有助于解决问题
#pragma once
#include <iostream>
#include <string>
#include "Plant.h"
using namespace std;
class Fruit : public Plant {
public:
Fruit (std::string& plant_name, int energy_value):
Plant (plant_name, energy_value){} //constructor pased to base class
~Fruit(){ } //destructor
//inherits basically everything from the Plant base class, makes leae nodes in the class tree easy to write and access
};
现在这是真正的麻烦制造者。这只狐猴完全定义了eat()函数,并从hunt()传递给它的Food*,从草食动物调用,并对其进行更多测试以查看它是否是水果(这是唯一种植狐猴可以吃)
#pragma once
#include "Animal.h"
#include "Herbivore.h"
#include "Plant.h"
#include "Fruit.h"
#include "Leaf.h"
#include "Nut.h"
#include <iostream>
#include <string>
#include <list>
#include <typeinfo>
using namespace std;
class Lemur : public Herbivore
{
public:
Lemur(void) : name(), alive(true), age(0), calories(0), weight(0) {}
Lemur(string& animal_name, int animal_age, int animal_calories, double animal_weight) :
name(animal_name), alive(true), age(animal_age), calories(animal_calories), weight(animal_weight), maxcalories(animal_calories) {}
~Lemur(){}
bool eat(Food* food)
{
if (Fruit* temp = dynamic_cast<Fruit*>(food))
{
//PROBLEM, it sees every plant as a fruit in this
//case...at least according to a typeinfo().name() that i have run in here. However the temp
//always returns null, so this proper if statement never actually happens, so it never sees
//any fruit, even though there's a whole bunch in the list (500 of them). what's wrong?
cout << "it's a fruit" << endl;
return true;
}
else
{
//cout << "not a fruit" << endl;
return false;
}
}
void PrintSelf()
{
cout << "i am a " << age << " year old, " << weight << " kilogram " << name << " with " << calories << " calories." << endl;
};
string& getName(){
return name;
};
std::string name;
bool alive;
int age, calories, maxcalories;
double weight;
};
如您所见,即使我已经确认它遍历列表,dynamic_cast 也永远不会返回非空临时值。我还使用计数器变量来跟踪它的进度,奇怪的是它说列表中有 1500 种植物……但是 0 种水果……
我的演员表结构错了吗?我的继承权取消了吗?做什么?
编辑; 我为每个类添加了虚拟析构函数,所以这不是问题