我知道我必須錯過其中一個細節,你不能只是看起來發現,然後在一個月後它擊中你「Evrika!」。抽象工廠 - 獲取LKN2019的死亡錯誤消息
基本上,我要做的是爲形狀實現一個抽象工廠類,但是我一直得到與從我的形狀工廠代碼創建的.obj相關的LKN2019錯誤消息。
它看起來像這樣一點:
shapefactory.h
#ifndef SHAPEFACTORY_H__
#define SHAPEFACTORY_H__
#include <iostream>
#include <map>
#include <string>
#include "shape.h"
#include "circle.h"
#include "rectangle.h"
#include "triangle.h"
using namespace std;
typedef Shape *(createShapeFunction)(void);
class ShapeFactory
{
public:
static void registerFunction(const string &, const createShapeFunction *);
static Shape *createShape(const string &);
static Shape *createShape(istream &);
private:
static map <string, createShapeFunction *> creationFunctions;
ShapeFactory();
static ShapeFactory *getShapeFactory();
};
#endif
shapefactory.cpp
#include "shapefactory.h"
void ShapeFactory::registerFunction(const string & ID, const crateShapeFunction * CR)
{
creationFunctions[ID] = CR;
}
Shape* ShapeFactory::createShape(const string & shapeName)
{
map <string, crateShapeFunction *>::iterator it = creationFunctions.find(shapeName);
if(it != creationFunctions.end())
return it->second();
return NULL;
}
Shape* ShapeFactory::createShape(istream & in)
{
string shapeName;
cout << "Desired figure: ";
in >> shapeName;
map <string, crateShapeFunction *>::iterator it = creationFunctions.find(shapeName);
if(it != creationFunctions.end())
return it->second();
return NULL;
}
ShapeFactory::ShapeFactory()
{
registerFunction("circle", &Circle::Create);
registerFunction("rectangle", &Rectangle::Create);
registerFunction("triangle", &Triangle::Create);
}
ShapeFactory* ShapeFactory::getShapeFactory()
{
static ShapeFactory instance;
return &instance;
}
我大概的方法實現搞砸了某個地方,但我只是看不到在哪裏。任何形式的及時幫助將不勝感激。
編輯:看了 錯誤這樣
錯誤4錯誤LNK2001:無法解析的外部符號「私人:靜態類的std ::地圖,一流的std ::分配器>,類Shape *(* __cdecl)(無效),struct std :: less,class std :: allocator >>,class std :: allocator,class std :: allocator> const,類Shape *(__cdecl *)(void)>>> ShapeFactory :: creationFunctions「(? creationFunctions @ @@ ShapeFactory 0V?$地圖@ V'$ basic_string的@ DU?$ char_traits @ d @ @@ STD V'$分配器@ d @ @@ 2 STD @@ P6APAVShape @@ XZU?$ @少V'$ basic_string的@DU?$ char_traits @ d @ @@ STD V'$分配器@ d @ @@ 2 STD @@@ 2 @ V'$分配器@ U&$ @配對$$ CBV?$ basic_string的@ DU?$ char_traits @ d @ std @@ V $ $ allocator @ D @ 2 @@ std @@ P6APAVShape @@ XZ @ std @@@ 2 @@ std @@ A)C:\ Proj1 \ shapefactory.obj
在定義我的標題中的地圖之前,請按照alexrider的建議。謝謝 - 它現在有效。
請包含**完整**錯誤消息。 「LKN2019」只是你得到的消息的一部分。 –
在頭文件中使用'namespace std;'可能會很快就會令人驚訝。 –