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

  • Home
  • SEARCH
  • 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 8512125
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T04:11:49+00:00 2026-06-11T04:11:49+00:00

My code is: class cMySingleton{ private: static bool bInstantiated; int mInt; cMySingleton(){ mInt=0; }

  • 0

My code is:

class cMySingleton{
private:
    static bool bInstantiated;
    int mInt;
    cMySingleton(){
        mInt=0;
    }
public:
    cMySingleton(int c){
        if (bInstantiated){
            cout << "you can only instantiated once";
        }
        else {
            cMySingleton();
            mInt=c;
        }
    }
};

int main () {

    cMySingleton s(5);
    cMySingleton t(6);
}

The linker keeps complaining:

Undefined symbols for architecture x86_64:
  "cMySingleton::bInstantiated", referenced from:
      cMySingleton::cMySingleton(int) in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

What is going on? C++ novice here~~

  • 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-06-11T04:11:50+00:00Added an answer on June 11, 2026 at 4:11 am

    you should initialize static field.

    http://ideone.com/Y1huV

    #include <iostream>
    class cMySingleton{
    private:
        static bool bInstantiated;
        int mInt;
        cMySingleton(){
            mInt=0;
        }
    public:
        cMySingleton(int c){
            if (bInstantiated){
                std::cout << "you can only instantiated once";
            }
            else {
                cMySingleton();
                mInt=c;
            }
        }
    };
    bool cMySingleton::bInstantiated = true;
    int main () {
    
        cMySingleton s(5);
        cMySingleton t(6);
    }
    

    More information you can be find here:

    Static Data Member Initialization

    there was also missing include and std:: around cout.

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

Sidebar

Related Questions

Here is the code: class Root { public static void main(String[] arguments) { int
Code: class B { public: B () : b(++bCounter) {} int b; static int
Sample code: class Sample{ private int v; public void setV(){ Lock a=new Lock(); a.lock();
Suppose one has this code: class MyClass { public: MyClass(int foo) : foo(foo){} private:
My code class Union { //Search Function static boolean search(int A[], int i) {
I have the following code: class Test { public static void main(String[] args) {
i have code: class foo { public: bar(const QString& s){} ..... }; int main()
this is my short code class A { public: int x,y; A(int a=0, int
Consider this code: class First { public: int y; First() { y = 90;
See code: class Foo { public static function dumpObj($obj) { var_dump($obj); } } class

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.