samedi 29 juillet 2017

getting linking Error while creating a flyweight_pattern

#include <iostream>
#include <map>
#include <string>
#include <cstdlib>

using namespace std;

class Shape 
{
public :
    virtual void draw()=0;
    virtual ~Shape(){}
};

class Circle : public Shape
{
   string color;
   int x;
   int y;
   int radius;
public:
  Circle(string color){
      color = color;        
   }

    void setX(int x) {
       x = x;
   }

    void setY(int y) {
      y = y;
   }

    void setRadius(int radius) {
      radius = radius;
   }
    void draw() {
     cout << "color :" << color << x << y ; 
   }
};

class ShapeFactory {
public:
    static map<string, Shape*> circlemap;
    static Shape* getcircle(string color)
    {
        Shape *mcircle;
        mcircle = circlemap.find(color)->second;
        if(mcircle == nullptr) {
         mcircle = new Circle(color);
         circlemap[color] = mcircle;
        // circlemap.insert(std::make_pair(color,mcircle));
        }
        return mcircle;
    }

};

class Flyweightpattern
{
public:

    static string getRandomColor(string colors[]) {
      int m_rand = rand() % 5; 
      return colors[m_rand];
   }
    static int getRandomX() {
      return (int)(rand() % 100);
   }
    static int getRandomY() {
      return (int)(rand() % 100);
   }

};

int main()
{
    string colors[] = { "Red", "Green", "Blue", "White", "Black" };

      for(int i=0; i < 20; ++i) {
         Circle *circle = dynamic_cast<Circle *>(ShapeFactory::getcircle(Flyweightpattern::getRandomColor(colors)));
         circle->setX(Flyweightpattern::getRandomX());
         circle->setY(Flyweightpattern::getRandomY());
         circle->setRadius(100);
         circle->draw();
      }

      getchar();
      return 0;
   }

I am getting the linking error during run is below :

flyweight_pattern.obj : error LNK2001: unresolved external symbol "public: static class std::map,class std::allocator >,class Circle *,struct std::less,class std::allocator > >,class std::allocator,class std::allocator > const ,class Circle *> > > ShapeFactory::circlemap" (?circlemap@ShapeFactory@@2V?$map@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@PAVCircle@@U?$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@@PAVCircle@@@std@@@2@@std@@A)

I have a map in the ShapeFactory class and tried to creating filling the map in the class itself but still not able to resolve the problem.

Aucun commentaire:

Enregistrer un commentaire