|
| rajesh msen (5) | |
| My singleton class looks like these #ifndef _SINGLETON_H #define _SINGLETON_H #include <stdio.h> #include <stdlib.h> #incldue "MyStuff.h" class Singleton{ public: MyStuff_class *mYstuff_Ptr; static Singleton* getInstance(){ if (instance == 0){ instance = new Singleton; } // end if return instance; } private: Singleton(){ void init(); } void init(){ mYstuff_Ptr = new MyStuff_class(); } static Singleton* instance; }; Singleton* Singleton::instance = 0; #endif /* _SINGLETON_H */ This above code is in one file I wants mYstuff_Ptr access to all other class in my project.(one instance can accessed globally) But my problem is above code works for only time Example: #include <iostream> #include "SINGLETON.h" int main() { //I able to acces MyStuff_class via this below code Singleton::getInstance()->mYstuff_Ptr; return 0; } //above code runs smoothly If i include "SINGLETON.h" file in other file(example "Graphics.h") #include <iostream> #include "SINGLETON.h" #include "Graphics.h" int main() { //I able to acces MyStuff_class via this below code Singleton::getInstance()->mYstuff_Ptr; return 0; } First Error : I got a this error "private : static class singleton::instance ... is already in main.obj Second Error: redefined... Can anyone know how to make mYstuff_Ptr accessble from anywhere in my project using singleton design pattern. Is it possible in Singleton design pattern Most of the articles about singleton class in the internet is like this #include <iostream> class singleton{ .... } int main() { //used inside main function } /// Please help me how to make mYstuff_Ptr globally accessble | |
| PanGalactic (778) | |
Singleton* Singleton::instance = 0;must be in a source file, not a header. | |
| rajesh msen (5) | |
| Thanks PanGalactic after adding it in source file it Works fine | |
This topic is archived - New replies not allowed.
