#include <iostream>
template <class T>
class singleton
{
public :
static T* instance() {sinstance = new T; return sinstance;}
private :
static T* sinstance;
singleton(singleton const&);
singleton& operator=(singleton const&);
protected:
singleton();
~singleton() {delete sinstance;}
};
template <class T> T* singleton<T>::sinstance=NULL;
class circle
{public:
circle( int radius) { pointer = new int; *pointer = radius ;}
circle( const circle &r) { pointer = new int; *pointer=*r.pointer;}
~circle() { delete pointer;}
circle& operator=(const circle&pr)
{ *pointer =*pr.pointer; }
int getData(){ return *pointer;}
private:
int *pointer;
};
typedef singleton<circle> circlesingleton;
int main()
{
circle(7);
circlesingleton::instance()->getData();
}
Hi. Im doing homework using singleton class .How to corect this ?Im a beginner. And these code are code I search online and add some part to it .Thanks. The first class in a singleton class which I use as a template. The circle class in a class our professor told us to use canonical form
Errors :
project1singleton.cpp: In instantiation of ‘static T*
singleton<T>::instance() [with T = circle]’:
project1singleton.cpp:40:19: required from here
project1singleton.cpp:7:51: error: no matching function for call to
‘circle::circle()’
static /*singleton*/ T* instance() {sinstance = new T /*singleton*/ ;return
sinstance;}
^
project1singleton.cpp:7:51: note: candidates are:
project1singleton.cpp:26:7: note: circle::circle(const circle&)
circle( const circle &r) { pointer = new int; *pointer=*r.pointer;}
^
project1singleton.cpp:26:7: note: candidate expects 1 argument, 0 provided
project1singleton.cpp:25:7: note: circle::circle(int)
circle( int radius) { pointer = new int; *pointer = radius ;}
^