mercredi 23 novembre 2016

singleton subclass constructor errors

I Tried to subclass a singleton and i encountered the error while i tried to print from the derived class constructor.

mysingleton.h

#ifndef mysingleton_h
#define mysingleton_h
#include <iostream>
class mysingleton{
  public:
   static mysingleton* singletoninstance; //The one and only one instance
   static mysingleton* getinstance();
   virtual void dosomething() = 0 ;

 protected:
   mysingleton(const mysingleton&);
   ~mysingleton();

   public:
      mysingleton(){

      }//Prevent clients from creating new singleton

   };



class mysingleton_child:public mysingleton
{
  public:
    virtual void dosomething() {
       std::cout <<"In Child Class \n";
     }
    mysingleton_child()
   {
    //   std::cout <<"In Child Class \n";

   }
~mysingleton_child();
};

mysingleton.cpp

#include "mysingleton.h"
mysingleton* mysingleton::singletoninstance = 0;

mysingleton* mysingleton ::  getinstance()
{
  if( !singletoninstance )
    singletoninstance = new  mysingleton_child();
  return singletoninstance;  
}

main func :

#include <iostream>
#include "mysingleton.h"
int main ()
{
   mysingleton *pointer  = mysingleton::getinstance();
   pointer->dosomething();
}

I get the below error when i uncomment the line from mysingleton_child constructor

// std::cout <<"In Child Class \n";

Error:

Error : Undefined symbols for architecture x86_64: "mysingleton::~mysingleton()", referenced from: mysingleton_child::mysingleton_child()

Why am i not able to write anything in the constructor ? Is the derived constructor called , or just the base classes constructor is called? Also, when i make the base classes destructor public everything seems to work fine. But if we have a public destructor , any would destroy the singleton.

Can you please help me with the explanation and make me understand better.

Aucun commentaire:

Enregistrer un commentaire