fitzbutz
fitzbutz

Reputation: 976

C++ Factory implementation trouble

I'm coding a simple c++ factory and I get an error that I don't understand. This is my class definition:

MSGFactory.hpp

class MSGFactory
{
   public:
       static MSGFactory * getInstance();
       void registerMSG(const std::string MSGType , createMSGFn constructor );
       MSGData *createMessage(const std::string &MSGType);

   private:
       static MSGFactory * inst;
       std::map<std::string,createMSGFn> MSGPool;
};

and this is its implementation:

MSGFactory.cpp

MSGFactory * MSGFactory::getInstance()
{
    if(inst == NULL) inst = new MSGFactory();

    return inst;
}

void MSGFactory::registerMSG(const std::string MSGType , createMSGFn constructor )
{
     MSGPool.insert(factoryBucket(MSGType,constructor));
}


 MSGData * MSGFactory::createMessage(const std::string &MSGType)
 {
      std::map<std::string,createMSGFn>::iterator it;   
      it = MSGPool.find(MSGType);

      if( it != MSGPool.end() )
      return it->second();
      return NULL;
  }  

I wrote this test program:

T_MSGFactory.cpp

class MSGOne : MSGData
{
    public:
    MSGOne(){}
    ~MSGOne() {{std::cout<<"Derived destructor called\n";}}
    std::string type() { std::cout << "Type One" << std::endl; return " ";}
    unsigned char* getData(){ return (unsigned char *) "a"; }
    static MSGData * Create() { return new MSGOne(); }

};


class MSGTwo : MSGData
{
    public:
    MSGTwo(){}
    ~MSGTwo() {std::cout<<"Derived destructor called\n";}
    std::string type() { std::cout << "Type Two" << std::endl; return " ";}
    unsigned char* getData(){ return (unsigned char *) "a"; }
    static MSGData * Create() { return new MSGTwo(); }
};


class MSGThree : MSGData
{
    public:
    MSGThree(){}
    ~MSGThree()    {std::cout<<"Derived destructor called\n";}
    std::string type() { std::cout << "Type Three" << std::endl; return " ";}
    unsigned char* getData(){ return (unsigned char *) "a"; }
    static MSGData * Create() { return new MSGThree(); }    
};



int main(int argc,  char **argv)
{


      std::cout << "PROVA" << std::endl;


      MSGFactory::getInstance()->registerMSG("ONE", &MSGOne::Create );
      MSGFactory::getInstance()->registerMSG("TWO", &MSGTwo::Create );
      MSGFactory::getInstance()->registerMSG("THREE", &MSGThree::Create );


      return 0;
}

but compiling it with "g++ T_MSGFactory.cpp MSGFactory.cpp" I get this error:

1) "riferimento non definito a" is "undefined reference to"

2) "nella funzione" is "in function"

compiling error

Can someone help me? Thanks

Upvotes: 0

Views: 104

Answers (2)

dari
dari

Reputation: 2455

Static member variables must be defined, usually in the source file.

Add this to your MSGFactory.cpp

MSGFactory* MSGFactory::inst = 0;

http://www.learncpp.com/cpp-tutorial/811-static-member-variables/

Upvotes: 2

SHR
SHR

Reputation: 8313

In the top of your MSGFactory.cpp file, just after the includes, in the global scope, you should declare the static member.

like this:

MSGFactory* MSGFactory::inst=NULL;

Upvotes: 2

Related Questions