0

I was practicing how to make a singleton. Everything was going just fine until I decided to play with it. the working version is:

sing.hp:

class GlobalClass
{
private:
    GlobalClass(int);
    int m_value;
    static GlobalClass *s_instance;
//    static GlobalClass instance;
  public:

    int get_value();
    void set_value(int v);
    static GlobalClass *GetS_instance();
//    static GlobalClass GetInstance();
};

sing.cpp

#include"sing.hpp"
#include<iostream>
using namespace std;

GlobalClass * GlobalClass::s_instance = 0;
//GlobalClass  GlobalClass::instance;

GlobalClass::GlobalClass(int v = 10)
{
    this->m_value = v;

}

int GlobalClass::get_value()
{
    return this->m_value;
}

void GlobalClass::set_value(int v)
{
    this->m_value = v;
}

GlobalClass * GlobalClass::GetS_instance()
{
    if (!s_instance)
    {
        s_instance = new GlobalClass;
    }
    return s_instance;
}

//GlobalClass  GlobalClass::GetInstance()
//{
//
//    return instance;
//}

main.cpp:

#include "sing.hpp"
#include<iostream>
using namespace std;

void foo(void)
{
  GlobalClass::GetS_instance()->set_value(1);
  cout << "foo: global_ptr is " << GlobalClass::GetS_instance()->get_value() << '\n';
}

int main()
{
    cout << "main: global_ptr is " << GlobalClass::GetS_instance()->get_value() << '\n';
      foo();
      return 1;
}

The above set up works well until I uncomment what I have commented: I get the following error:

~/workspace/singleton2$ g++ *.cpp -o dr
sing.cpp:6:27: error: no matching function for call to ‘GlobalClass::GlobalClass()’
sing.cpp:6:27: note: candidates are:
sing.hpp:5:2: note: GlobalClass::GlobalClass(int)
sing.hpp:5:2: note:   candidate expects 1 argument, 0 provided
sing.hpp:2:7: note: GlobalClass::GlobalClass(const GlobalClass&)
sing.hpp:2:7: note:   candidate expects 1 argument, 0 provided

Apart from not knowing why this occurred, the strange thing is that: the first line of error message points to line 6 in my program where I have defined the instance, while the error is indicating a mistake in my constructor. could you please help me how to solve this issue? thank you all

rahman
  • 4,820
  • 16
  • 52
  • 86

1 Answers1

1

You returning by value this generates a copy which does not work as you don't want Singeltons to have copy constructors.

Change this to return by reference:

GlobalClass  &  GlobalClass::GetInstance()
   //       ^^^
{
    return instance;
}

While you are here you should make sure you disable the copy constructor and assignment operator

Update your default parameter is in the wrong place.

GlobalClass::GlobalClass(int v = 10)
             //               ^^^^^ Remove this
{
    this->m_value = v;
}


private:
    GlobalClass(int v = 10);
            //      ^^^^^^   Add this

See: C++ Singleton design pattern

Community
  • 1
  • 1
Martin York
  • 257,169
  • 86
  • 333
  • 562
  • Hi, I did that, but didn't work(same error). I also added some line(hope to be correct) to sing.hpp to disable the copy constructor and assignment operator : GlobalClass(const GlobalClass&); GlobalClass& operator=(const GlobalClass&); – rahman Mar 09 '12 at 04:47