2016-12-05 76 views
0

我有一個文件,我正在讀取要創建的對象類型,金額和價格。我已經驗證過文件正在被正確讀取,並且這些值被放置在變量類型,數量和價格中,但是程序會獲取Checkout.cpp中的第一個糖果構造函數,並在創建該對象之前停止並退出。 Candy和Cookie類繼承自DessertItem基類,並且向量「甜點」類型爲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 :: 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(){ 
} 
+0

http://stackoverflow.com/help/mcve – melpomene

+0

輸出什麼做你現在 – Erix

+0

輸出「1型條件」(第一行該文件是類型1),然後退出 –

回答

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) 
{ 
    // ... 
} 
+0

我改變了這個部分,但我仍然在製作對象時遇到了麻煩。我在構造函數中放了一個cout來測試它,但它永遠不會在那裏。它輸出一切,直到對象初始化。 –

+0

「它從來沒有做到」,你的意思是你的程序崩潰?如果您想對此發表評論,您應該爲您的問題添加更多代碼。也許你正在調用基類構造函數的純虛函數。那會很糟糕。 – paddy

+0

我更新了上面的代碼。即使嘗試創建糖果對象也不會因爲某些原因而在Checkout類的循環範圍內成功。 –