c++ - 如何从另一个文件 : C++ 访问类的公共(public)枚举

标签 c++ enums

这是我的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 类型,尽管它被定义为 public

我试着让它成为 static 以及 extern 但没有成功。

我做错了什么?

最佳答案

此错误是由拼写错误引起的。使用

void orderPizza(SimplePizzaFactory::PizzaType type); // Uppercase 'T' in PizzaType.

代替

void orderPizza(SimplePizzaFactory::Pizzatype type);

关于c++ - 如何从另一个文件 : C++ 访问类的公共(public)枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23644831/

相关文章:

c++ - 升级到较新版本后 boost fusion/mpl 问题

java - 访问实现接口(interface)的通用枚举的父方法

c++ - 在C++中模仿OpenCL float2类型

c++ - Qt:子对象可以在其父对象中组合吗?

swift - 在 Swift wrt 消息类型中注册接收者对象

MySQL三态字段

ruby-on-rails - 如何将枚举作为字符串存储到 Rails 中的数据库

c# - 如何测试按位枚举是否包含 C# 中另一个按位枚举的任何值?

c++ - 访问函数的段错误

c++ - 错误的实例化功能本身并未实例化。 clang和gcc之间的行为差​​异