0

这涉及一些非常棘手的继承,但请耐心等待。我的问题不是一个具体的错误,而只是“我将如何具体做到这一点?”

这个想法是有一个抽象的基类 Food (请注意,这对于问题来说都过于简单了)

//parent of Animal
//parent of Plant
//~Food()
//Food()
#pragma once

class Food
{
public:
    Food(){}
    ~Food(){}



};

由此产生类动物和植物。我现在不太担心植物 动物需要有虚拟功能 Hunt and Eat

#pragma once
#include "Food.h"


class Animal : public Food
{
//eat() which accepts a Food* type as an argument. it is an abstract virtual in this class, it has the form
//bool eat(Food* food)

//hunt() which accepts an stl list of Food* pointers to Food type objects. the food list is declared globally in main and passed here. it has the form
//hunt(list<Food*> &foodlist)
};

由此产生更多的课程;Herbivore, Carnivore, Omnivore(继承自肉食动物和草食动物)。这是草食动物

//Child of Animal
//Parent of Lemur, Koala, Squirrel, Omnivore

//~Herbivore()
//hunt(list<Food*&foodList):bool (only eats plant types)
#pragma once
#include "Animal.h"
#include <iostream>
#include <string>
#include <list>
using namespace std;

class Herbivore : public virtual Animal
    {
    public:

        Herbivore() {}
        ~Herbivore(){}
        //eat() and hunt() are probably virtual here as well, as they aren't used directly, only the lower classes directly access them


};

从那些是最底层的子类,它们都大致具有这种形式。这是松鼠

//child of Herbivore
//leaf node

#pragma once
#include "Animal.h"
#include "Herbivore.h"

class Squirrel : public Herbivore
{ 

        //bool eat() is fully defined here instead of being virtual.
        //bool hunt() is fully defined here instead of being a virtual.

       //both have the same argument lists as the virtuals in Animal



};

这是主要的

list<Food*> Food_list; //global list of Food items that will be passed to hunt()
int main()
{

    list<Food*>::iterator it = Food_list.begin();

    (*it)->eat(*it); //passing the iterator to itself as a test. this seems to work ok
    (*it)->hunt(Food_list); //however this, in my code, refuses to work for a few reasons

};

所以基本上一切都从食物继承......但这是一件坏事。

我已经尝试了以下几个问题

我尝试了Animal中虚拟功能的初始版本,食物中没有,它抱怨食物没有功能狩猎

error C2039: 'hunt' : is not a member of 'Food' 

....我想这是公平的,虽然它不应该看松鼠而不是食物类吗?

我尝试在 Food 中创建一个纯虚拟来吃和打猎,从那时起,每次尝试实例化任何类型的叶子类(如松鼠或老虎或其他)都会返回“无法实例化抽象类”错误。

error C2259: 'Squirrel' : cannot instantiate abstract class

我试着让食物中的吃和狩猎不那么抽象,比如狩猎(列表和食物列表),但是它说“语法错误,标识符'列表'”,就像它不知道列表是什么......即使在我之后包含在 Food.h 中

error C2061: syntax error : identifier 'list'

并且所有这些错误都与错误“'Food::hunt': function does not take 1 arguments”配对

error C2660: 'Food::hunt' : function does not take 1 arguments

我的总体问题是,您如何将这个抽象虚函数从 Animal 转换为它的叶类?你会怎么称呼它?基本上我所尝试的一切都惨遭失败 *不要担心eat()或hunt()里面有什么,我只是在寻找正确的声明*

这个项目的 github 也可以在这里 https://github.com/joekitch/OOP_JK_Assignment_4 如果需要的话

4

2 回答 2

0

我找到的解决方案涉及动态转换。基本上,您需要将迭代器指针 DOWN 从 Food* 类型转换为较低的类型,例如 Herbivore 或 Animal 类型,无论哪种方式,该类型都必须具有您想要在其中完全定义的功能

Herbivore* temp = dynamic_cast<Herbivore*>(*it)
if ( temp ){
        cout << "iterator thing is a Herbivore " << endl;
        temp->hunt(Food_list);
    cout << "iterator thing is of the type " << typeid(temp).name() << endl;}
    else cout << "iterator is not a Herbivore " << endl;}

上面的代码将尝试将其转换为 Herbivore类型。如果成功(即它的父类是 Herbivore),那么 Temp 将被转换为左侧指定的 Herbivore 类型。如果失败,则 temp 将是 NULL 类型。这个临时指针指向与 it 指针相同的东西....但它被简单地视为草食动物而不是食物*。

于 2013-05-09T13:38:51.340 回答
0

一些想法,

  • 我假设 Herbivore 是在某个地方定义的......
  • 使用虚拟析构函数
  • 当某事告诉你它不能在某处被实例化时,就会调用 Food(), Animal() 构造函数。

示例代码:

class Food
{
public:
    Food(){ }
    virtual ~Food(){ }
};

class Animal : public Food
{
 Animal() : Food() { }
 virtual Animal() { } //Cause C++

 virtual bool eat(Food* food) = 0;
 virtual hunt(list<Food*> &foodlist) = 0;
};

class Squirrel : public Herbivore
{ 
 Squirrel() : Herbivore() { }
 ~Squirrel() { } //not virtual

  bool eat(Food *food) { //stuff };
  void hunt(list<Food *> &foodlist) { //stuff };
};

list<Animal*> animal_list; //global list of Food items that will be passed to hunt()
int main()
{
    animal_list.push_back(new Squirrel()); // Make sure you fill the array?

    list<Food*>::iterator it = Food_list.begin();

    (*it)->eat(*it); //passing the iterator to itself as a test. this seems to work ok
    (*it)->hunt(animal_list); //however this, in my code, refuses to work for a few reasons

};
于 2013-05-09T02:56:45.777 回答