C++ 如何从另一个文件访问类的公共枚举:C++;
这是我的C++ 如何从另一个文件访问类的公共枚举:C++;,c++,enums,C++,Enums,这是我的simplepizza工厂.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 SimpleP
simplepizza工厂.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
这是我的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 "SimplePizzaFactory.h"
Pizza* SimplePizzaFactory::createPizza(PizzaType type)
{
switch(type){
case cheese:
return new cheesePizza();
case veggie:
return new veggiePizza();
}
throw "Invalid Pizza Type";
}
#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类型
,尽管它被定义为public
我试着使它成为静态的
以及外部的
,但没有成功
我做错了什么?这个错误是由打字错误引起的。使用
void orderPizza(SimplePizzaFactory::PizzaType type); // Uppercase 'T' in PizzaType.
而不是
void orderPizza(SimplePizzaFactory::Pizzatype type);
此错误是由输入错误引起的。使用
void orderPizza(SimplePizzaFactory::PizzaType type); // Uppercase 'T' in PizzaType.
而不是
void orderPizza(SimplePizzaFactory::Pizzatype type);