不允许使用抽象类类型"Animal"的对象:(C++)

jca*_*los 1 c++ visual-c++

我正试图将东西存入矢量,但问题很少.这些是我的代码.

Animal.cpp

#include "Animal.h"
#include "Cattle.h"
#include "Sheep.h"
#include <iostream>
#include <string>
using namespace std;

Animal::Animal(int newid, double newweight, int yy, int mm, int dd, double newaccDose, char newsex)
{
    id = newid;
    weight = newweight;
    yy = yy;
    mm = mm;
    dd = dd;
    dose = newaccDose;
    sex = newsex;
}

Animal::Animal()
{
    id = 0;
    weight = 0;
    yy = 0;
    mm = 0;
    dd = 0;
    dose = 0;
    sex = ' ';
}

Animal::~Animal(){}

double Animal::getDaysDifference(){
    jdate dateOfBirth(dd,mm,yy);
    jdate now;
    double diff = now-dateOfBirth;
    return diff;
}

void Animal::addAnimal(){
    int select=0;
    int id;

    cout << "1.  Cattle    2.  Sheep" << endl;
    cout << "Select a type of animal: ";
    cin >> select;

    if(select==1){
        Cattle* c1;
        cout << "Please answer following questions" << endl;
        cout << "Type the animal's ID: ";
        cin >> id;

        c1->setID(id);

        //vector<Animal> vec;
        //vector<Animal>::iterator vec_ite;

    }else if(select==2){
        Sheep* s1;
        cout << "Please answer following questions" << endl;
        cout << "Type the animal's ID: ";
        cin >> id;

        s1->setID(id);
    }
    else{
        cout << "Invalid number please try again" << endl;  
    }
}
Run Code Online (Sandbox Code Playgroud)

首先,在上述代码中,Cattle* c1并且Sheep* s1,它不具有误差.即使我编码喜欢s1->setID(id)它也没关系.然而,

Cattle.cpp

#include "Cattle.h"

Cattle::Cattle(int newid, double newweight, int yy, int mm, int dd, 
           double newdose, char newsex, string newcategory)
    : Animal(id, weight, yy,mm,dd, dose, sex)
{
    id = newid;
    weight = newweight;
    dose = newdose;
    sex = newsex;
    Cattle::category = newcategory;
}

Cattle::Cattle(){
    id = 0;
    weight = 0;
    dose = 0;
    sex = ' ';
    Cattle::category = "";
}

Cattle::~Cattle(){}

string Cattle::getCategory(){
    return category;
}

double Cattle::calcDose(){
    Cattle* c1;
    if(c1->getDaysDifference() < 90 || c1->getCategory() == "Meat"){
        c1->dose = 0;
        return c1->dose;
    }
    else if(c1->getCategory() == "Dairy"){
        if (c1->weight < 250 || c1->dose > 200){
            c1->dose = 0;
        }
        else{
            c1->dose = c1->weight * 0.013 + 46;
        }
        return c1->dose;
    }
    else if(c1->getCategory() == "Breeding"){
        if (c1->weight < 250 || c1->dose > 250){
            c1->dose = 0;
        }
        else{
            c1->dose = c1->weight * 0.021 + 81;
        }
        return c1->dose;
    }
    else
    {
        cout << "It is not valid category" << endl;
    }
}
Run Code Online (Sandbox Code Playgroud)

Animal.h

#ifndef ANI_H
#define ANI_H
#include <vector>
#include "Treatment.h"
#include "jdate.h"

class Animal{
protected:
    int id;
    double weight;
    int yy;
    int mm;
    int dd;
    double dose;
    char sex;
    //Treatment treatArray[];
public:
    Animal();
    Animal(int newid, double newweight, int yy, int mm, int dd, double newdose, char newsex);
    ~Animal();

    virtual double calcDose() = 0;
    void addAnimal();
    double getDaysDifference();
};
#endif
Run Code Online (Sandbox Code Playgroud)

DrugAdmin.cpp(主要)

#include <iostream>
#include "Animal.h"
#include "Cattle.h"
using namespace std;

int main(){
    Animal* a1=new Animal;
    delete a1;

    a1->addAnimal();
}
Run Code Online (Sandbox Code Playgroud)

问题是在这里,当我尝试定义Animal* a1=new Animal; delete a1;它时说Object of abstract class type "Animal" is not allowed: 我已经谷歌搜索和Binged在这里和那里,我想我不能定义抽象基类本身但子类.所以我在Cattle.cpp使用中尝试了同样的事情,Cattle c1=new Cattle;但它也不起作用..

我真的不确定那是什么问题..你能帮帮我吗?

干杯

Kar*_*tel 5

virtual double calcDose()

这说" calcDose()对于动物的一切都是可能的,不同的动物可能会以不同的方式做到......"

= 0;

"......但是没有'通用'方式为动物做这件事;每个人必须指定自己的方法".

您可能无法构造一种未指定的Animal类型,因为根据定义,缺少一条关键信息:如何calcDose使用它.

这一切都很好.机会是,你真的不想建造一个未指明的动物,原因同样在现实生活中,你不会去农场(动物园?宠物商店?)购买"动物"而没有任何特定的偏见你想要什么样的动物

Animal存在类型,以便您可以在适当的时候省略有关动物类型的信息.在您的情况下,创建实例不是其中之一.


而且,无论你认为它有多少,addAnimal都绝对没有意义作为成员的功能Animal.有三个基本问题:

1)因为它是一个非静态成员函数,所以你需要Animal调用该函数.那是一种捕获22.

2)你没有地方给动物补充.你需要某种容器.

3)从概念上讲,制作动物清单并不是动物的责任.这是谁需要列表的责任.

4)一般来说,在要求用户提供信息的代码与作用于该信息的代码之间进行清晰分离是一个好的策略.写一个免费的(意思是,不是类的一部分)函数,询问信息,构造Animal并返回指针; 然后在调用代码中,将指针存储在容器中.

除非你真的不想使用原始指针.您应该使用某种智能指针,或者将指针放入其中一个boost中ptr_container.