undefined reference

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
  2 #include <new>
  3 
  4 
  5 using namespace std;
  6 
  7 
  8 class Singleton
  9 {
 10 private:
 11         static Singleton *instance;
 12         static bool flag;
 13         Singleton();
 14 public:
 15         static Singleton* getMyInstance();
 16         void display(){cout<<"im using it"<<endl;}
 17 };
 18 
 19 
 20 Singleton* Singleton::instance = NULL;
 21 
 22 Singleton* Singleton::getMyInstance()
 23 {
 24         if(!instance)
 25         {
 26                 try
 27                 {
 28                         instance = new Singleton;
 29                 }catch (bad_alloc xa){cout<<"allocation failed"<<endl;exit(0);}
 30                 cout<<"new object only created once"<<endl;
 31         }       
 32         return instance;
 33 }       
 34 
 35 int main()
 36 {
 37 Singleton *obj, *obj1;
 38 obj = Singleton::getMyInstance();
 39 obj->display();
 40 cout<<"************"<<endl;
 41 
 42 obj1 = Singleton::getMyInstance();
 43 obj1->display();
 44 return 0;
 45 }



singleton.cpp:28: undefined reference to `Singleton::Singleton()'
collect2: ld returned 1 exit status
Your Signleton class contains pointers to itself, not some data type like I think you wanted.

Since you do not have a constructor for the class, line 28 is illegal because you are trying to create a new instance.
line 13 doesn't have a body like so: Singleton() { }
Topic archived. No new replies allowed.