很抱歉,如果我发布了太多代码。我真的不知道链接错误来自哪里。这条线似乎有问题:
dough = ingredientFactory->createDough();
在CheesePizza类中,但我不知道是否有feedFactory引用或者是Dough类中的问题。任何人都可以告诉我为什么它不会链接? (我收到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;
}
};
答案 0 :(得分:1)
(如果我没记错的话,这来自Head First:设计模式,c ++ - ized)
您的问题是您已在PizzaIngredientFactory
中声明了一些函数,但尚未定义它们,导致链接器引发错误消息。您可以通过查看错误来确定原因。 visual studio链接器将在LNK2001消息中列出它们。
您可以通过在PizzaIngredientFactory
中将这些函数声明为虚拟来解决此问题。正如Joachim Pileborg所暗示的那样,纯虚拟对于“普通”虚拟是有利的,因为这是抽象工厂的模式
另外要记住的一件事是,为继承而设计的类应该有一个虚拟析构函数(但它也必须在基类中定义,否则会发生另一个链接器错误。)