2016-12-05 11 views
0

作成するオブジェクトのタイプ、金額、価格を読んでいるファイルがあります。私は、ファイルが正しく読み取られていることを確認し、値は変数の型、数量、価格に配置されていますが、プログラムはCheckout.cppの最初のキャンディーコンストラクターに到達し、オブジェクトを作成する前に停止して終了します。 CandyクラスとCookieクラスはDessertItem基本クラスから継承し、ベクトル "desserts"はDessertItem型です。 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); 
}; 

キャンディコンストラクタで

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

関連コード

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

私はこの部分を変更しましたが、最初はオブジェクトを作成することにもまだ問題があります。私はそれをテストするためにコンストラクタに勘定書を入れましたが、決してそれを作ることはありません。オブジェクトの初期化までのすべてを出力します。 –

+0

「それは決してできません」という意味では、あなたのプログラムがクラッシュすることを意味しますか?あなたがコメントしたいのであれば、あなたの質問にコードを追加してください。おそらく、あなたは基本クラスのコンストラクタから純粋な仮想関数を呼び出しているでしょう。それは悪くなるだろう。 – paddy

+0

上記のコードを更新しました。 Candyオブジェクトを作成しようとしても、Checkoutクラスのループの範囲内で何らかの理由で成功しません。 –

関連する問題