#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();
}
您好。我正在使用单件类做作业。如何核心这个?我是初学者。这些代码是我在线搜索的代码,并添加了一些部分。谢谢。我用作模板的单例类中的第一个类。我们教授告诉我们使用规范形式的班级中的圆圈课程
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 ;}
^
答案 0 :(得分:0)
您的class circle
没有默认(无参数)构造函数,因此new T
中的class singleton
无效。