-2

あなたが見つけられないように見える詳細の1つを私が見逃しているに違いないことを私は知っています.

基本的に、シェイプの抽象ファクトリ クラスを実装する必要がありますが、シェイプ ファクトリ コードから作成された .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

シェイプファクトリー.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: 未解決の外部シンボル "private: static class std::map,class std::allocator >,class Shape * (__cdecl*)(void),struct std::less,class std::allocator > >, class std::allocator,class std::allocator > const ,class Shape * (__cdecl*)(void)> > > ShapeFactory::creationFunctions" (?creationFunctions@ShapeFactory@@0V?$map@V?$basic_string@DU ?$char_traits@D@std@@V?$allocator@D@2@@std@@P6APAVShape@@XZU?$less@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@ D@2@@std@@@2@V?$allocator@U?$pair@$$CBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@ @P6APAVShape@@XZ@std@@@2@@std@@A) C:\Proj1\shapefactory.obj

alexrider が提案したように、ヘッダーでマップを定義する前に。ありがとう - それは今動作します。

4

1 に答える 1

1

これの定義が欠けているstatic map <string, createShapeFunction *> creationFunctions;
場合は、追加することで修正できます

map <string, createShapeFunction *> ShapeFactory::creationFunctions; 

shapefactory.cpp
BTW に実際のエラー メッセージを貼り付けると役立ちます。

于 2013-04-14T19:56:49.910 に答える