Reputation: 2975
This is my Pizaa.h
#pragma once
#ifndef PIZZA_H
#define PIZZA
#include <iostream>
#include <string>
#include <list>
#include <limits>
#include <vector>
#include <cstdlib>
using namespace std;
/****************************
Abstract Base Pizza Class
*****************************/
class Pizza{
string name;
string dough;
string sauce;
std::list<string> topping;
public:
Pizza(string name,string dough,string sauce);
void prepare();
virtual void bake()=0;
virtual void cut()=0;
void box();
string getName();
void addTopping(string str);
void getTopping(std::list<string>& lst);
virtual ~Pizza();
};
#endif
This is cheesePizza.h
#pragma once
#ifndef CHEEZEPIZZA_H
#define CHEEZEPIZZA_H
#include <iostream>
#include <string>
#include <list>
#include <limits>
#include <vector>
#include <cstdlib>
#include "Pizza.h"
using namespace std;
class cheesePizza:public Pizza{
public:
cheesePizza();
~cheesePizza();
virtual void bake();
virtual void cut();
};
#endif
This is veggiePizza.h
#pragma once
#ifndef VEGGIEPIZZA_H
#define VEGGIEPIZZA_H
#include <iostream>
#include <string>
#include <list>
#include <limits>
#include <vector>
#include <cstdlib>
#include "Pizza.h"
class veggiePizza:public Pizza{
public:
veggiePizza();
~veggiePizza();
virtual void bake();
virtual void cut();
};
#endif
And this is my Factory class:
#include <iostream>
#include <string>
#include "Pizza.h"
#include "cheesePizza.h"
#include "veggiePizza.h"
using namespace std;
class SimplePizzaFactory{
public:
enum PizzaType {
cheesePizza,
veggiePizza
};
Pizza* createPizza(PizzaType type)
{
switch(type){
case cheesePizza:
// Pizza* p = new cheesePizza(); ##Here
//delete(p); ##Here
case veggiePizza:;
// return new veggiePizza();
}
throw "Invalid Pizza Type";
}
};
int main()
{
Pizza* p = new cheesePizza();
delete(p);
return 0;
}
My SimplePizzaFactory
method createPizza()
is not able to find new cheesePizza()
& new veggiePizza()
and I get below error if I uncomment #Here
$ g++ -Wall -c SimplePizzaFactory.cpp -o SimplePizzaFactory.o
SimplePizzaFactory.cpp: In member function ‘Pizza* SimplePizzaFactory::createPizza(SimplePizzaFactory::PizzaType)’:
SimplePizzaFactory.cpp:19:33: error: expected type-specifier before ‘cheesePizza’
Pizza* p = new cheesePizza();
^
SimplePizzaFactory.cpp:21:8: error: jump to case label [-fpermissive]
case veggiePizza:;
^
SimplePizzaFactory.cpp:19:25: error: crosses initialization of ‘Pizza* p’
Pizza* p = new cheesePizza();
^
SimplePizzaFactory.cpp:17:8: warning: enumeration value ‘veggiePizza’ not handled in switch [-Wswitch]
switch(type){
All my files i.e *.o
& *.cpp
are placed in the same folder.
I am not able to figure out why this error. The same code
Pizza* p = new cheesePizza();
delete(p);
inside main()
works fine, but if you uncomment #Here
it gives compilation error.
Upvotes: 0
Views: 69
Reputation: 17127
You are declaring cheesePizza both as an element of an enumeration and as a class, probably the enumeration element gets precedence.
Rename one of the two to solve your problem.
The reason that it works in main is that there the cheesePizza enumeration element is out of scope, so there is no nameclash there.
If you would have reduced your program to a minimal example exhibiting the problem you would most likely have found the problem yourself.
Upvotes: 2