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

结帐

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


糖果

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 :: 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(){
}

最佳答案

您不能通过值将多态类型存储在容器中。调用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 )
{
    // ...
}

关于c++ - 无法从输入文件创建对象,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40967311/

10-11 18:05