1

I have a class defined like somewhat below where copy constructor and assignment operator are deleted. QAC is throwing a warning that "This class has declared default or deleted special members but no destructor". Why is the warning and how to I solve it?

class A
{
  public:
     static A& getInstance()
     {
        static A AInstance;
        return AInstance;
     }
     A(A const&) = delete;
     void operator=(A const&) = delete;
   private:
     A();
};
273K
  • 29,503
  • 10
  • 41
  • 64
Salvankar
  • 91
  • 6

1 Answers1

1

The linker warning shows what is wrong:

In function A::getInstance()': <source>:8: undefined reference to A::A()'

The fix is simple:

class A
{
  public:
     static A& getInstance()
     {
        static A AInstance;
        return AInstance;
     }
     A(A const&) = delete;
     A& operator=(A const&) = delete;  //< fix 2

   private:
     A() = default;  //< fix 1 (C++11)
};

Fix 1: Provide the definition (let the compiler do it by using "=default" or use {} for old C++)

Fix 2: see https://en.cppreference.com/w/cpp/language/copy_assignment

Fix 3: (if you want to make it explicit) also delete the move constructor and the move assignment operator.

Notes:

Sonic78
  • 680
  • 5
  • 19