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 7635231
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T07:23:09+00:00 2026-05-31T07:23:09+00:00

I am getting some errors when I try to put declaration and definition of

  • 0

I am getting some errors when I try to put declaration and definition of a class into separatte hpp and cpp file. could you help me fix it please.
I am trying to manipulate a singleton like this:

sing.hpp:

class GlobalClass {
    int m_value;
    static GlobalClass *s_instance;
    GlobalClass(int);

  public:
    int get_value();
    void set_value(int v);
    static GlobalClass *instance(); };

sing.cpp:

#include"sing.hpp"
GlobalClass::GlobalClass(int v = 0)
{
    this->m_value = v;
}

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

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

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

main.cpp:

#include "sing.hpp"
int main()
{
    GlobalClass *s=0;
}

command and errors are:

~/workspace/singleton$ g++  main.cpp sing.cpp 
sing.cpp: In function ‘GlobalClass GlobalClass::* instance()’:
sing.cpp:19:10: error: ‘s_instance’ was not declared in this scope
sing.cpp:2:1: error: ‘GlobalClass::GlobalClass(int)’ is private
sing.cpp:20:23: error: within this context
sing.cpp:21:12: error: ‘s_instance’ was not declared in this scope
  • 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-31T07:23:10+00:00Added an answer on May 31, 2026 at 7:23 am

    Your definition of instance has two errors:

    1. The static qualifier is erroneous.
    2. The syntax for the return type got scrambled. The pointer belongs to the type, not the name of the function:

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

    Furthermore, you also need to define the static member s_instance as others have noted.

    GlobalClass* GlobalClass::s_instance = 0;
    

    But this code has another problem: it leaks memory. Don’t use raw pointers.

    Finally, this code isn’t thread safe and this may in some situtaions be a huge problem. Assuming that you can guarantee that your code is never going to run in multi-threaded scenarios, go ahead. Otherwise, you probably want to change it (and who can offer such a strong guarantee anyway?).

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

Sidebar

Related Questions

I'm new in subsonic3, and I'm getting some errors when I try to use
I'm getting some really wierd linking errors from a class I wrote. I am
I am getting some errors thrown in my code when I open a Windows
I'm deploying a webpart on sharepoint and getting some errors. The webpart consumes data
I'm working on a school project and I'm getting some weird errors from Xcode.
So, I'm getting some compile errors on netbeans 6.5 generated web service code for
I'm getting some very wierd errors. The compiler seems to want to call the
Getting these errors in some java code http://tutorials.jenkov.com/java-multithreaded-servers/multithreaded-server.html Exception in thread Thread-0 java.lang.RuntimeException: Cannot
I want to log some seemingly random errors I'm getting in a Delphi written
I am getting 'trailing whitespace' errors trying to commit some files in Git. I

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.