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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T19:59:15+00:00 2026-05-14T19:59:15+00:00

Below code instantiates a derived singleton object based on environment variable. The compiler errors

  • 0

Below code instantiates a derived singleton object based on environment variable. The compiler errors saying error C2512: 'Dotted' : no appropriate default constructor. I don’t understand what the compiler is complaining about.

EDIT:
Fixed issues with implementing the get instance method which requires definition of both the parent and derived class. By separating the class definitions in separate header files and including the same in Singleton.cpp where the instance function is implemented.

Mainfile – 1
#include <iostream>
#include <string>
#include "Singleton.h"
using namespace std;

int main(){

     Singleton::instant().print();
     cin.get();
}
Singleton.h
#pragma once
#include <iostream>
using std::cout;
class Singleton{
public:
    static Singleton & instant();
    virtual void print(){cout<<"Singleton";}
protected:
    Singleton(){};
private:
    static Singleton * instance_;
    Singleton(const Singleton & );
    void operator=(const Singleton & );

};

Singleton.cpp
#include "Singleton.h"
#include "Dotted.h"
Singleton * Singleton::instance_ = 0;
Singleton & Singleton::instant(){
    if (!instance_)
    {
        char * style = getenv("STYLE");
        if (style){
            if (strcmp(style,"dotted")==0)
            {
                instance_ = new Dotted();
                return *instance_; 
            }           else{
                instance_ = new Singleton();
                return *instance_;
            }       
        }

        else{
            instance_ = new Singleton();
            return *instance_;
        }
    }
    return *instance_;

}
Dotted.h

#pragma once
class Dotted;

class Dotted:public Singleton{
public:
    friend class Singleton;
    void print(){cout<<"Dotted";}
    private:
        Dotted(){};

};
  • 1 1 Answer
  • 1 View
  • 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-14T19:59:16+00:00Added an answer on May 14, 2026 at 7:59 pm

    There are several problems with your code:

    • You mean to return type Singleton& or const Singleton&. You are currently returning by value, which is attempting to invoke a copy constructor, and no such constructor exists.
    • Your default constructor in Dotted probably is not available in Singleton. I suggest you make Singleton a friend of Dotted so that it is able to access that constructor. Although not 100% sure on this one.
    • You forgot to make the function print() virtual, so your override won’t manifest itself.
    • You have put “friend” in the wrong place; you need to declare Singleton a friend of Dotted in Dotted, not within Singleton.
    • You should not make your definition of Singleton::instant inline as it needs to construct an instance of Dotted and, in order to do that, it needs to see Dotted’s definition. So you should move that to a source file where it is able to see both Dotted and Singleton’s complete definitions, respectively.
    • You need to put Singleton* Singleton::instance_ = 0; in a source file somewhere.
    • You are missing an else clause in your if(!style) section; currently, if the STYLE environment variable is set, but isn’t set to “dotted”, then you end up returning a null singleton.

    In addition to the above, I strongly advise you to avoid environment variables and singletons. Both of them are examples of “shared mutable state” and can lead to a whole lot of messiness. Singletons, though they have appeared in “design pattern” books for quite some time, are now being understood to be design anti-patterns. It is a much more flexible approach to have an interface that you pass around and simply happen to instantiate once rather than bake in the fact that it exists once into its API.

    For example, for your particular case, I would suggest something like the following:

    class Printer
    {
        public:
            virtual ~Printer(){}
            virtual void print()const = 0
    };
    
    class StringPrinter : public Printer
    {
        public:
             StringPrinter() : _str("") {}
             StringPrinter(const std::string& str) : _str(str) {}
             StringPrinter(const StringPrinter& o) : _str(o._str) {}
             virtual ~StringPrinter(){}
             virtual void print()const{ std::cout << _str << std::endl; }
             StringPrinter& operator=(const StringPrinter& o){ _str = o._str; return *this;}
        private:
             std::string _str;         
    };
    

    Then, in any class where you previously used Singleton, simply take a const Printer& object. And print to that object. Elsewhere, you can conditionally construct a StringPrinter(“Singleton”) or StringPrinter(“dotted”). Or possibly some other instance of that interface, although I would suggest using QSettings or some sort of configuration file in place of environment variables, or at least use MYAPPLICATIONNAME_STYLE instead of just STYLE; in other words, if you are going to go the environment variable route, at least qualify its name.

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

Sidebar

Related Questions

Below code saying error incorreect syntax near Main INSERT INTO tbl ( 'Week', Main,
Why is the below code giving a compile time error. Map<String,? extends Object> inputMap
The below code give me a foreach error. What appears to be the issue
Referencing the code below, I have a list of Error objects that has been
Below code not work, but it's work fine for jsf1.2. Now the framework is
below code is my databasehandler class i got it from a tutorial. Beside that
Below code : URL oracle = new URL(http://www.oracle.com/); URLConnection inputStream =oracle.openConnection(); InputStream in =
Below code makes form to be submitted without html5 validation... $j(document).on(click, #client_entry_add, function(event){ ajax_submit();});
In below code when I click 'Vote' a vote results screen is displayed but
The below code doesn't it print the value. function go(x) { alert(x.options.selectedIndex.value); //location=document.menu.student.options[document.menu.student.selectedIndex].value }

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.