作成するオブジェクトのタイプ、金額、価格を読んでいるファイルがあります。私は、ファイルが正しく読み取られていることを確認し、値は変数の型、数量、価格に配置されていますが、プログラムは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(){
}
http://stackoverflow.com/help/mcve – melpomene
何出力あなたが今得るか – Erix
それはの「タイプ1の条件」(最初の行を出力ファイルはタイプ1です)、その後終了します –