Error deleting an object
Dec 10, 2016 at 9:44pm UTC
halo guys i have a question, why i have error in my deconstructor if i have 2 mode.
if i only have
CocktailSimulator* cocktailSimulatorOBJ = new CocktailSimulator();
cocktailMachinePTR = cocktailSimulatorOBJ;
in this case input = 1
there is no error
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
#include "Menue.h"
#include <cstdlib>
Menue::Menue()
{
int input ;
cout << " ------- Select Mode -------- " << endl;
cout << "1. Real Mode " << endl;
cout << "2. Simulator Mode " << endl;
cout << "0. Exit " <<endl;
cout << "Auswahl => " ;
cin >> input;
if (input == 1)
{
CocktailSimulator* cocktailSimulatorOBJ = new CocktailSimulator();
cocktailMachinePTR = cocktailSimulatorOBJ;
}else if (input == 2)
{
CocktailBot* cocktailBotOBJ = new CocktailBot();
cocktailMachinePTR = cocktailBotOBJ;
}
}
Menue::~Menue()
{
cout << "Dekonstruktor Menue " << endl;
system("pause" );
delete cocktailMachinePTR;
//delete cocktailSimulatorOBJ;
//delete cocktailBotObj;
}
void Menue::menue()
{
char choice = ' ' ;
do {
system("cls" );
cout << "== CocktailMix | V0.2 ==" << endl;
cout << "1 - Make Cocktails" << endl;
cout << "2 - Configure CocktailMix" << endl;
cout << "0 - Exit" << endl << endl;
cout << "Auswahl=> " ;
cin >> choice;
if (choice == '1' )
{
submenue_makeCocktail();
}
else if (choice == '2' )
{
submenue_configure();
}
} while (choice != '0' );
}
void Menue::submenue_makeCocktail()
{
int choice = 0;
do {
system("cls" );
cout << "== CocktailMix | Mix it ==" << endl;
if (cocktailMachinePTR->getCocktails().size() != 0) {
for (unsigned int i = 0; i < cocktailMachinePTR->getCocktails().size(); i++) {
cout << i + 1 << " - " << cocktailMachinePTR->getCocktails()[i]->getName() << endl;
}
}
else
{
cout << "No Cocktails saved" << endl;
}
cout << "0 - Exit" << endl << endl;
cout << "Auswahl=> " ;
cin >> choice;
if (choice > 0 && choice <= cocktailMachinePTR->getCocktails().size()) {
system("cls" );
cocktailMachinePTR->makeCocktail(cocktailMachinePTR->getCocktails()[choice-1]);
cout << endl << "Press enter to return" ;
cin.ignore();
string tmp;
getline(cin, tmp);
}
} while (choice != 0);
}
void Menue::submenue_configure()
{
char choice = ' ' ;
do {
system("cls" );
cout << "== CocktailMix | Configure ==" << endl;
cout << "1 - Configure Dispensers" << endl;
cout << "2 - List Cocktail" << endl;
cout << "3 - Add Cocktail" << endl;
cout << "4 - Edit Cocktail" << endl;
cout << "5 - Delete Cocktail" << endl;
cout << "0 - Exit" << endl << endl;
cout << "Auswahl=> " ;
cin >> choice;
switch (choice)
{
case '1' :
submenue_configureDispencer();
break ;
case '2' :
break ;
case '3' :
break ;
case '4' :
break ;
case '5' :
break ;
default :
break ;
}
} while (choice != '0' );
}
void Menue::submenue_configureDispencer()
{
int choice = 0;
do {
system("cls" );
cout << "== CocktailMix | Configure Dispenser ==" << endl;
for (int i = 0; i < cocktailMachinePTR->getDispensers().size(); i++) {
cout << i + 1 << " - " << cocktailMachinePTR->getDispensers()[i]->getIngredient()->getName() << endl;
}
cout << "0 - Exit" << endl << endl;
cout << "Auswahl=> " ;
cin >> choice;
if (choice > 0 && choice <= cocktailMachinePTR->getDispensers().size()) {
cocktailMachinePTR->getDispensers()[(choice - 1)]->setIngredient(submenue_selectIngredient());
}
} while (choice != 0);
}
Ingredient* Menue::submenue_selectIngredient()
{
int choice = 0;
system("cls" );
cout << "== CocktailMix | Select Ingredient ==" << endl;
for (int i = 1; i < cocktailMachinePTR->getIngredients().size(); i++) {
cout << i << " - " << cocktailMachinePTR->getIngredients()[i]->getName() << endl;
}
cout << "0 - Free / Exit" << endl << endl;
cout << "Auswahl=> " ;
cin >> choice;
if (choice > 0 && choice < cocktailMachinePTR->getIngredients().size())
{
return cocktailMachinePTR->getIngredients()[(choice)];
}
else {
return cocktailMachinePTR->getIngredients()[0];
}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
#pragma once
#include "Ingredient.h"
#include "Dispenser.h"
#include "Cocktail.h"
#include "CocktailMachine.h"
#include "CocktailSimulator.h"
#include "CocktailBot.h"
#include <iostream>
using namespace std;
class Menue
{
public :
Menue();
virtual ~Menue();
void menue();
void submenue_makeCocktail();
void submenue_configure();
void submenue_configureDispencer();
Ingredient* submenue_selectIngredient();
private :
CocktailMachine* cocktailMachinePTR;
CocktailBot* cocktailBotObj;
CocktailSimulator * cocktailSimulatorOBJ;
};
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#ifndef COCKTAILSIMULATOR_H
#define COCKTAILSIMULATOR_H
#include "CocktailMachine.h"
class CocktailSimulator : public CocktailMachine
{
public :
void makeCocktail(Cocktail *);
protected :
private :
};
#endif // COCKTAILSIMULATOR_H
1 2 3 4 5 6 7 8 9 10 11 12 13
#ifndef COCKTAILBOT_H
#define COCKTAILBOT_H
#include "CocktailMachine.h"
class CocktailBot : public CocktailMachine
{
public :
void makeCocktail(Cocktail *);
};
#endif // COCKTAILBOT_H
Last edited on Dec 10, 2016 at 11:48pm UTC
Dec 10, 2016 at 11:33pm UTC
virtual ~Menue();
¿what does virtual do?
¿when should a destructor be virtual?
The problem is in CocktailMachine, but those are the concepts.
By the way
1 2 3 4
private :
CocktailMachine* cocktailMachinePTR;
CocktailBot* cocktailBotObj;
CocktailSimulator * cocktailSimulatorOBJ;
¿why do you have those three variables?
Edit: I think I misread, don't see your CocktailMachine class definition.
Please provide a complete testcase.
Last edited on Dec 10, 2016 at 11:34pm UTC
Dec 10, 2016 at 11:49pm UTC
hi ne555
this is my CocktailMachine.h
by the way that 3 variables for my constructor Menue
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
#pragma once
#include "Cocktail.h"
#include "Dispenser.h"
#include "Ingredient.h"
#include <vector>
#include <fstream>
#include <cstdlib>
using namespace std;
class CocktailMachine
{
public :
//Kon- und Destruktor
CocktailMachine();
virtual ~CocktailMachine();
//Funktion
virtual void makeCocktail(Cocktail *) = 0;
//setter
void setCocktails(Cocktail*);
//getter
vector<Ingredient*> getIngredients();
vector<Dispenser*> getDispensers();
vector<Cocktail*> getCocktails();
private :
Ingredient* ingredientObj;
Dispenser* dispenserObj;
Cocktail* cocktailObj;
vector<Ingredient*> ingredients;
vector<Dispenser*> dispensers;
vector<Cocktail*> cocktails;
};
Topic archived. No new replies allowed.