This is homework…I’m not asking for answers, I just have a bug I’m not sure what to do with. Thanks!
The bug in question probably has nothing to do with the assignment itself, but here is the assignment description anyways:
I’m working on an assignment (in C++) meant to teach use of the decorator design pattern through the classic example of a pizza with toppings. (My professor may as well have lifted it straight from http://simplestcodings.com/2010/12/26/decorator-design-pattern-example-ni-c/). I’m running into a little problem that I was wondering if someone could help me with.
I have a main menu(pizzeria) object that takes input from the user and performs the desired actions on a pizza. Users start with a basic pizza and then can add toppings to it until they’re done. So the first thing that my “newPizza” function does is declare the new Pizza as a Plain, which is a subclass of abstract class Pizza.
Then they get to enter their choice of toppings. Each time, a pointer to the same Pizza object is sent to the addToppings() function, the new decoration is added, and the pointer is returned. Each decoration inherits from a price category, which inherits from pizzaToppings, which inherits from Pizza.
This is the relevant part of the main order function:
Pizza* Menu::newPizza()
{
cout << "\nNew Pizza";
//accept the next choice
int choose = 0;
//create the new pizza
Plain * currentPizza = new Plain();
//until they choose to end the order
while (choose != 3)
{
//accept the choice
cin >> choose;
switch (choose)
{
//if they want to add a new topping
case 1:
{
//add topping to current pizza
//and this is where the problem is spotted by the compiler
addTopping(currentPizza);
break;
}
The issue is that when I try to send the pointer currentPizza to the function addTopping(), I get
“Run-Time Check Failure #3 – The variable ‘currentPizza’ is being used without being initialized.”
Didn’t I just initialize it right there on line 7?
If I hit “continue”, the program keeps going, and works, but I get that same error every time I call the function. Is it just a syntax error somewhere, or do I have some actual issue here?
Thanks!!
[edit:]
The addTopping() function:
Pizza* Menu::addTopping(Pizza* thisPizza)
{
cout << "\nAdd topping";
//declare choose int
int choose = 0;
//accept number of topping
cin >> choose;
//decide which one to add
switch (choose)
{
//mozzarella
case 1:
{
thisPizza = new Mozzarella(thisPizza);
break;
}
//mushrooms
case 2:
{
thisPizza = new Mushrooms(thisPizza);
break;
}
//another 13 possible toppings, won't bore you with the details ;)
}
cout << "\nEnd add topping\n";
return thisPizza;
}
Do you have
currentPizzaalso declared as an field of thePizzaclass and you are using that somewhere else? If so, thecurrentPizzayou are updating innewPizzais specific to that method, and you need to do justcurrentPizza = new Plain();instead of declaring a newcurrentPizzavariable in the scope of the method.Also, in your
addToppingmethod, you are only updating the argumentthisPizza, which is a copy of the pointercurrentPizza.You need to do: