无法从输入文件创建对象

时间:2016-12-05 04:53:42

标签: c++

我有一个文件,我正在阅读要创建的对象类型,金额和价格。我已经验证文件正在被正确读取并且值被放置在变量类型,数量和价格中,但是程序到达Checkout.cpp中的第一个糖果构造函数并在创建对象之前停止并退出。 Candy和Cookie类继承自DessertItem基类,矢量“desserts”属于DessertItem类型。

Checkout.h

class Checkout{

private:
    std::vector<DessertItem*> dessertList;
    std::string fileToOpen;
    int type;
    double quantity;
    double price;

public:
    //constructor
    Checkout(std::string);
    //read file
    std::ifstream desserts{};
    //sets
    void setFileName(std::string);
    //gets
    std::string getFileName();
    //read file and add correct object to vector
    void readFile();
    //recipt display method
    void displayReceipt();
};

Checkout.cpp中的相关代码

   while(desserts >> type >> quantity >> price){
        if(type==1){
            std::cout << "type 1 condition" << '\n';
            std::cout << price << '\n';
            //gets to here and then crashes
            Candy* candy = new Candy(quantity,price);
            dessertList.push_back(candy);
        }

Candy.h

class Candy : public DessertItem{
private:
    double weight;
    double priceLB;
    const std::string itemType= "Candy";
public:
    //construtor
    Candy(double,double);
    //destructor
    ~Candy();
    //sets
    void setWeight(double);
    void setPrice(double);
    //gets
    double getWeight();
    double getPrice();
    //virtual print
    virtual void print();
    //virtual calculate cost
    double calculateCost(double,double);
};

Candy Constructor

Candy :: Candy(double weight, double priceLB):DessertItem(itemType, calculateCost(weight,priceLB)){
    setWeight(weight);
    setPrice(priceLB);
    std::cout << "made candy" << '\n';
}

DessertItem.h

class DessertItem{
private:
    std::string dessertName;
    double dessertCost;
public:
    //construtor
    DessertItem(std::string, double);
    //destructor
    ~DessertItem();
    //sets
    void setName(std::string);
    void setCost(double);
    //gets
    std::string getName();
    double getCost();
    //virtual print
    virtual void print();
    //virtual calculate cost
    virtual double calculateCost();
};

DessertItem.cpp

//constructor accepting 1 argument
DessertItem :: DessertItem(string name, double cost){
    setName(name);
    setCost(cost);
}
//destructor
DessertItem :: ~DessertItem(){}
//sets
void DessertItem :: setName(string name){
    dessertName=name;
}
void DessertItem :: setCost(double cost){
    dessertCost=cost;
}
//gets
string DessertItem:: getName(){
    return dessertName;
}
double DessertItem :: getCost(){
    return dessertCost;
}
//virtual print
void DessertItem :: print(){

}
//virtual calculate cost method
double DessertItem :: calculateCost(){
}

1 个答案:

答案 0 :(得分:3)

您无法按容器中的值存储多态类型。致电push_back时实际发生的情况是您的价值在被添加之前被转换为DessertItem

要在容器中使用多态,它必须存储引用或指针。您可以将存储定义为:

std::list<std::unique_ptr<DessertItem>> dessertList;

然后:

dessertList.emplace_back( std::make_unique<Cookie>( quantity, price ) );
dessertList.emplace_back( std::make_unique<Candy>( quantity, price ) );

你的循环也不是很好。不要测试eof。循环的一个天真的修复是将其转换如下:

while( desserts >> type >> quantity >> price )
{
    // ...
}