问题的结构是这样的 食物是一个抽象基类;植物和动物直接从它那里继承。 食草动物,食肉动物和杂食动物继承动物, 而水果和坚果和叶从植物 继承狐猴,考拉和松鼠继承草食动物dynamic_cast只能正确执行
总体而言,它是一个炎热的烂摊子,但它是必要的锻炼。 整个项目可以在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(){ }
};
下一个是动物,它包含了追捕(的虚函数),吃()函数
#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()中的注释)。狩猎需要在全球范围内主要宣布的类型Food *的列表()
#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;
};
};
这里是Fruit类。没有什么特别明显的,我只是把它放在这里只是在情况下,他们可以帮助解决这个问题
#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()函数,并且从狩猎()传递给它的食物*,从草食动物中调用,并且对它做了更多的测试,看看它是否是水果(这是唯一的植物狐猴可以吃)
#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个水果......
我在构造我的演员是错的吗?我的遗传是关闭的吗?做什么?
编辑;我添加的虚拟析构函数为每个类,所以这是不看的github回购问题
啊哈!那一定是它。这也是有意义的,植物下面什么也没有,所以演员总是会失败,因为首先在植物下面没有任何东西 – user2364502 2013-05-10 10:58:22