2012-02-08 75 views
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; 
    } 
}; 
+1

它必须在LNK2001之前。你有没有为类定义所有的方法? 'PizzaIngredientFactory'函数应该被声明为虚拟。 – zeller 2012-02-08 08:47:52

+3

有两件事:'PizzaIngredientFactory'中的成员函数应该是虚拟的,甚至可以是纯虚拟的。第二件事是'PizzaIngredientFactory'和'NYPizzaIngredientFactory'中'createSauce'的声明不同。 – 2012-02-08 08:49:11

+0

为了保持一致性,我把'this->'放在'面团'前面。 – Zenexer 2012-02-08 08:50:30

回答

1

(如果我没有记错这个来自深入浅出:设计模式,C++ - 源化)
你的问题是,你已经宣布PizzaIngredientFactory 一些功能,但还没有定义它们,导致链接器引发错误信息。您可以通过查看错误来确定原因。视觉工作室链接器将在LNK2001消息中列出它们。
您可以通过在PizzaIngredientFactory中声明这些函数为虚拟来解决此问题。正如Joachim Pileborg所说,纯粹的虚拟有利于“平淡”的虚构,因为这是抽象工厂。
另一件需要牢记的事情是,为继承设计的类应该有一个虚拟析构函数(但它也必须在基类中定义,否则会发生另一个链接器错误)。