Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7629641
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:53:12+00:00 2026-05-31T05:53:12+00:00

I was practicing how to make a singleton. Everything was going just fine until

  • 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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T05:53:13+00:00Added an answer on May 31, 2026 at 5:53 am

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I just started practicing TDD in my projects. I'm developing a project now using
I'm on railcasts just practicing some rails and have come across something I'm trying
I'm just starting out with rails and I'm practicing the basic ruby commands, one
I'm practicing recursion using Java and I've hit a problem. I'm trying to make
While practicing the jxl API, i cant extract details from certain Excel sheet its
I have been practicing TDD and (some) XP for a few years now and
I am practicing making a web app which tries to read the user's twitter
I have been practicing TDD in C# for several years now and recently have
I've been practicing for an upcoming programming competition and I have stumbled across a
I'm practicing Jquery and I've written this simple Jquery statement: var someText = $(table

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.