-1
 #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 ;}
   ^
tiger
  • 25
  • 6
  • 1
    Possible duplicate of [Why can templates only be implemented in the header file?](http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file). – πάντα ῥεῖ Sep 07 '15 at 00:17
  • 1
    You haven't posted the errors, in any case `sinstance = new T` isn't going to work for `circle` because `circle` doesn't have a default constructor, and `circle& operator=(const circle& pr)` doesn't `return` anything. – user657267 Sep 07 '15 at 00:26
  • @user657267 hi.I just add my errors. So every time I call the getData(), the default of circle will execute? But if I add a circle() {return *pointer;} the error is project1singleton.cpp:24:26: error: returning a value from a constructor circle() {return *pointer ;} – tiger Sep 07 '15 at 00:54
  • Your singleton does not get deleted. You should use `auto_ptr` or `unique_ptr` to ensure that it does. – Neil Kirk Sep 07 '15 at 01:19
  • @NeilKirk what about I move the ~singleton to public ?Is that OK?Thanks – tiger Sep 07 '15 at 01:27
  • Something in your code needs to destroy the object. Where is that? – Neil Kirk Sep 07 '15 at 02:27

1 Answers1

0

Your class circle has no default (no argument) constructor, therefore new T is not valid in class singleton.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436