如何从另一个文件访问类的公共枚举:C ++

时间:2014-05-14 02:45:17

标签: 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 type,尽管它被定义为public

我尝试将其设为static以及extern,但没有成功。

我做错了什么?

1 个答案:

答案 0 :(得分:4)

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

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

而不是

void orderPizza(SimplePizzaFactory::Pizzatype type);