2
對不起,如果我發佈了太多的代碼。我真的不知道鏈接錯誤來自哪裏。這似乎是有這個線路有問題:LNK1120類錯誤(工廠模式)。不知道它來自哪裏
dough = ingredientFactory->createDough();
在CheesePizza類
,但我不知道是否ingredientFactory引用或問題在麪糰類。任何人都可以告訴我爲什麼它不會鏈接? (我得到一個LNK1120錯誤)。任何幫助將不勝感激。
謝謝。
class Dough
{
string m_dough;
public:
Dough() : m_dough("Unknown dough") {};
string setDough(string dough) { m_dough = dough; };
string getDough() { return m_dough; };
};
class ThinCrustDough : public Dough
{
public:
ThinCrustDough() { setDough("Thin crust dough"); };
};
class PizzaIngredientFactory
{
public:
PizzaIngredientFactory() {};
Dough *createDough();
Sauce createSauce();
Cheese *createCheese();
};
class NYPizzaIngredientFactory : public PizzaIngredientFactory
{
Cheese *cheese;
public:
NYPizzaIngredientFactory(){};
Dough *createDough() { return new ThinCrustDough; };
Sauce *createSauce() { return new MarinaraSauce; };
Cheese *createCheese() { return new ReggianoCheese; };
};
class Pizza
{
string m_size;
string m_description;
string m_type;
PizzaIngredientFactory *ingredientFactory;
string m_name;
string m_dough;
string m_sauce;
string m_cheese;
Dough dough;
Sauce sauce;
Cheese cheese;
public:
Pizza() : m_description("Unknown Pizza"), m_size(" ,Unknown Size ") {};
Pizza(PizzaIngredientFactory *ingredientFactory){ this->ingredientFactory = ingredientFactory; };
string name() { return m_name; };
string getName() { return m_name; };
void setName(string name) { m_name = name; };
string getDescription() { return m_description; };
string setDescription(string setdescription) { return m_description = setdescription; };
string getSize() { return m_size; };
string setSize(string setsize) { return m_size = setsize; };
string getType() { return m_type; };
string setType(string settype) { return m_type = settype; };
virtual void prepare() = 0;
void bake();
void cut();
void box();
void orderPizza();
};
void Pizza::prepare()
{
Pizza *pizza;
cout << "Preparing " << pizza->getName() << endl;
cout << "Adding toppings : " << endl;
for (vector<string>::iterator itr = toppings.begin();
itr != toppings.end();
++itr)
{
cout << " " << *itr;
}
};
void Pizza::bake()
{
cout << "Bake for 25 minutes at 350 degrees" << endl;
};
void Pizza::cut()
{
cout << "Cutting the pizza into diagonal slices" << endl;
};
void Pizza::box()
{
cout << "Place pizza in official PizzaStore box" << endl;
};
class CheesePizza : public Pizza
{
PizzaIngredientFactory *ingredientFactory;
Pizza *pizza;
Dough dough;
Sauce *sauce;
Cheese *cheese;
public:
CheesePizza() { };
CheesePizza(PizzaIngredientFactory *ingredientFactory){ this->ingredientFactory = ingredientFactory; };
void prepare(){
cout << "Preparing " << getName() << endl;
dough = ingredientFactory->createDough();
}; ;
};
class PizzaStore
{
PizzaIngredientFactory *factory;
public:
PizzaStore() {};
PizzaStore(PizzaIngredientFactory *factory) { this->factory = factory; };
Pizza *orderPizza(string type)
{
Pizza *pizza;
pizza = createPizza(type);
pizza->prepare();
pizza->bake();
pizza->cut();
pizza->box();
return pizza;
}
protected:
virtual Pizza *createPizza(string type) = 0;
};
class NYPizzaStore : public PizzaStore
{
Pizza *pizza;
public:
NYPizzaStore() {};
NYPizzaStore(Pizza *pizza){ this->pizza = pizza; };
protected:
Pizza *createPizza(string item)
{
Pizza *pizza = NULL;
PizzaIngredientFactory *ingredientFactory = new NYPizzaIngredientFactory;
string type = "New York Style";
string cheese = "cheese";
if (strcmp(cheese.c_str(), item.c_str()) == 0)
{
pizza = new CheesePizza(ingredientFactory);
pizza->setType(type + " Cheese Pizza");
}
return pizza;
}
};
它必須在LNK2001之前。你有沒有爲類定義所有的方法? 'PizzaIngredientFactory'函數應該被聲明爲虛擬。 – zeller 2012-02-08 08:47:52
有兩件事:'PizzaIngredientFactory'中的成員函數應該是虛擬的,甚至可以是純虛擬的。第二件事是'PizzaIngredientFactory'和'NYPizzaIngredientFactory'中'createSauce'的聲明不同。 – 2012-02-08 08:49:11
爲了保持一致性,我把'this->'放在'麪糰'前面。 – Zenexer 2012-02-08 08:50:30