簡體   English   中英

不允許使用抽象類類型為“動物”的對象:(C ++)

[英]Object of abstract class type “Animal” is not allowed: (C++)

我正在嘗試將內容存儲到vector中,但是問題很少。 這些是我的代碼。

動物.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;  
    }
}

首先,在上面的代碼Cattle* c1Sheep* s1 ,沒有錯誤。 即使我像s1->setID(id)這樣編碼,也沒關系。 然而,

牛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;
    }
}

動物

#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

DrugAdmin.cpp(主要)

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

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

    a1->addAnimal();
}

問題出在這里,當我嘗試定義Animal* a1=new Animal; delete a1; Animal* a1=new Animal; delete a1; 它說Object of abstract class type "Animal" is not allowed:我在這里和那里都進行了Googled和Binged處理,我發現我不能定義抽象基類本身,而是子類。 因此,我使用Cattle c1=new Cattle;Cattle.cpp嘗試了相同的操作Cattle c1=new Cattle; 但這也不起作用。

我真的不確定這是怎么回事。請您能幫我嗎?

干杯

virtual double calcDose()

這就是說:“對於所有動物而言,都有可能使用calcDose() ,而不同的動物可能會做不同的事情……”

= 0;

“……但是沒有針對動物的'通用'方法;每個人都必須指定自己的方法”。

您可能不會構造某種未指定的Animal,因為根據定義,缺少一條關鍵信息:如何為其計算calcDose

這一切都很好。 很有可能,您實際上並不想構造一個未指定的動物,原因與現實生活中您沒有任何特定的先入之見就不會去農場(動物園?寵物店?)購買“動物”的原因相同。您想要哪種動物。

存在Animal類型,因此您可以在適當的時候省略有關哪種動物的信息。 在您的情況下,創建實例不是其中一種情況。


另外,無論您想做什么, addAnimal作為Animal的成員函數絕對沒有意義。 存在三個基本問題:

1)由於它是一個非靜態成員函數,因此您已經需要Animal來調用該函數。 那有點像22。

2)您沒有任何地方可以將動物添加到中 您需要某種容器。

3)從概念上講,列出動物名單不是動物的責任 任何需要清單的人都有責任。

4)通常,將請求用戶提供信息的代碼與作用於該信息的代碼區分開來是一個很好的策略。 編寫一個免費的 (意思是,不是類的一部分)函數來請求信息,構造Animal並返回該指針; 然后在調用代碼中,將指針存儲在容器中。

除非您真的不想為此使用原始指針 您應該使用某種智能指針,或者將指針放入boost ptr_container

Animal是一個抽象類,這意味着它具有虛函數。 這是Animal的純虛函數。

virtual double calcDose() = 0;

無法創建抽象類的對象。 這不僅適用於C ++,而且適用於所有支持虛函數的面向對象的語言(某些語言,例如C#,將其稱為abstract方法)。

如果刪除聲明中=0的純說明符,如下所示:

virtual double calcDose() = 0;
                       //^^^^ this is called pure specifier

那么它將變成虛擬函數(與虛擬函數相反)。 現在,您必須定義函數,然后可以創建Animal對象,因為它不再是抽象類。

Animal ,您具有:

virtual double calcDose() = 0;

因此該類是抽象的,無法實例化。 您只能構造從其派生實現該方法的類的對象。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM