2014-05-14 94 views
0

這是我SimplePizzaFactory.h如何從其它文件訪問類的公共枚舉:C++

#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){ 

所有文件都在同一個文件夾中,但儘管它被定義爲public仍是不能夠找到SimplePizzaFactory::Pizzatype type

我試過讓它static以及extern但沒有成功。

我在做什麼錯?

回答

4

此錯誤是由錯字造成的。使用

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

,而不是

void orderPizza(SimplePizzaFactory::Pizzatype type); 
+0

你的代碼中有'Pizzatype',而不是'PizzaType'。 –

+0

傻我..謝謝 –