这是我的SimplePizzaFactory.h
#pragma once
#ifndef SIMPLE_PIZZA_FACTORY_H
#define SIMPLE_PIZZA_FACTORY_H
#include <iostream>
#include <string>
#include "Pizza.h"
#include "cheesePizza.h"
#include "veggiePizza.h"
using namespace std;
class SimplePizzaFactory{
public:
enum PizzaType {
cheese,
veggie
};
Pizza* createPizza(PizzaType type);
};
#endif
我的
SimplePizzaFactory.cpp
#include "SimplePizzaFactory.h"
Pizza* SimplePizzaFactory::createPizza(PizzaType type)
{
switch(type){
case cheese:
return new cheesePizza();
case veggie:
return new veggiePizza();
}
throw "Invalid Pizza Type";
}
这是我的
PizzaStore.h
#pragma once
#ifndef PIZZA_STORE_H
#define PIZZA_STORE_H
#include "SimplePizzaFactory.h"
class PizzaStore{
SimplePizzaFactory* factory;
public:
PizzaStore(SimplePizzaFactory* factory);
void orderPizza(SimplePizzaFactory::Pizzatype type);
};
#endif
这是我的
PizzaStore.cpp
#include "PizzaStore.h"
using namespace std;
PizzaStore::PizzaStore(SimplePizzaFactory* factory){
this->factory=factory;
}
void PizzaStore::orderPizza(SimplePizzaFactory::Pizzatype type){
Pizza* pizza=factory.createPizza(type);
Pizza->prepare();
Pizza->bake();
Pizza->cut();
Pizza->box();
}
当我尝试编译
PizzaStore.cpp
时,出现以下错误:$ g++ -Wall -c PizzaStore.cpp -o PizzaStore.o
In file included from PizzaStore.cpp:1:0:
PizzaStore.h:10:37: error: ‘SimplePizzaFactory::Pizzatype’ has not been declared
void orderPizza(SimplePizzaFactory::Pizzatype type);
^
PizzaStore.cpp:12:49: error: variable or field ‘orderPizza’ declared void
void PizzaStore::orderPizza(SimplePizzaFactory::Pizzatype type){
^
PizzaStore.cpp:12:29: error: ‘Pizzatype’ is not a member of ‘SimplePizzaFactory’
void PizzaStore::orderPizza(SimplePizzaFactory::Pizzatype type){
所有文件都在同一文件夹中,但是尽管将其定义为
SimplePizzaFactory::Pizzatype type
,但仍然找不到public
。我尝试将其设置为
static
以及extern
,但均未成功。我究竟做错了什么?
最佳答案
此错误是由错字引起的。使用
void orderPizza(SimplePizzaFactory::PizzaType type); // Uppercase 'T' in PizzaType.
代替
void orderPizza(SimplePizzaFactory::Pizzatype type);