I get compile errors when building my singleton class in qt5.15.0. The following minimal example can produce my problem.
Singleton.h
#ifndef SINGLETON_H
#define SINGLETON_H
#include <memory>
class Singleton
{
public:
typedef std::shared_ptr<Singleton> Ptr;
static Ptr GetInstance();
private:
Singleton() = default;
~Singleton() = default;
static Ptr _singleton;
};
#endif // SINGLETON_H
Singleton.cpp
#include "singleton.h"
Singleton::Ptr Singleton::_singleton = nullptr;
Singleton::Ptr Singleton::GetInstance()
{
if(!_singleton)
{
_singleton = std::make_shared<Singleton>();
}
return _singleton;
}
And, a main.cpp for passing building process.
#include <QCoreApplication>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
return a.exec();
}
When I build in QtCreator, I get following errors:
'constexpr Singleton::Singleton()' is private within this context
...
'Singleton::~Singleton()' is private within this context
...
What's wrong with my codes?