Help with singleton needed!
Help with singleton needed!
I need some help fixing my singleton. I've been reading some papers about singletons all over the web but I just can't seem to get them working
So I would be glad if someone with a little experience with the singleton design pattern would please have a look at my source and try pointing out what im doing wrong.
The source might be kinda messy due to the many tests I've been doing lately and it's not all too well commented so if you see anything else in there please leave a post or two
The source should be here: http://www.zoneone.dk/PubDebug/
Thanks in advance!
/Saku
So I would be glad if someone with a little experience with the singleton design pattern would please have a look at my source and try pointing out what im doing wrong.
The source might be kinda messy due to the many tests I've been doing lately and it's not all too well commented so if you see anything else in there please leave a post or two
The source should be here: http://www.zoneone.dk/PubDebug/
Thanks in advance!
/Saku
Call me Wice, Miami Wice!
Sorry about that..
ATM I'm having trouble finding a way to get a working pointer to my singleton. I think I'm getting the singleton declared the right way in the NewOrder.cpp but then when I try to make i pointer in the other classes of the engine it ofcourse colides. I've tried with derived classes and all other things I could think of but I think I'm just messing it more up.
So if it's posible I would appreciate if you'd check if the fundamentals of my singleton is correct.
But for some reason my VC++ just crashes without any kind of errors everytime I start it so Im trying to get my source working in Code::Blocks. Ill return as soon as I get this fixed.
ATM I'm having trouble finding a way to get a working pointer to my singleton. I think I'm getting the singleton declared the right way in the NewOrder.cpp but then when I try to make i pointer in the other classes of the engine it ofcourse colides. I've tried with derived classes and all other things I could think of but I think I'm just messing it more up.
So if it's posible I would appreciate if you'd check if the fundamentals of my singleton is correct.
But for some reason my VC++ just crashes without any kind of errors everytime I start it so Im trying to get my source working in Code::Blocks. Ill return as soon as I get this fixed.
Call me Wice, Miami Wice!
hmm, your code seems to work for me, not sure what could be wrongSaku wrote: So if it's posible I would appreciate if you'd check if the fundamentals of my singleton is correct.
Code: Select all
#include <irrlicht.h>
#include <iostream>
class CCore {
private:
CCore() {
}
static CCore* m_instance;
public:
static CCore* Instance() {
if(m_instance == 0)
{
m_instance = new CCore();
}
return m_instance;
}
void displayHello() {
printf("hello");
}
};
CCore* CCore::m_instance = 0;
int main() {
CCore * Core = CCore::Instance(); //OMG! A Singleton!!!111oneone... :O
Core->displayHello();
}
You seem to be right. But I guess the real problem is I have no idea how to use my singleton outside the first class in which the singleton as declared (neworder.cpp).
My singleton is build as the ones in the papers and they never describe how to use the singleton in multiple files.
I've seen it done in IrrTrueAxis made by Spintz. But I think he uses some kind of derived classes. I've tried to dublicate his pattern but I just can't seem to get it right.
Im actually quite confused and not too sure of what im doing wrong here. Sorry.
And yes I have been working in VC++ and now Code::Blocks trying to debug my way through this.
My singleton is build as the ones in the papers and they never describe how to use the singleton in multiple files.
I've seen it done in IrrTrueAxis made by Spintz. But I think he uses some kind of derived classes. I've tried to dublicate his pattern but I just can't seem to get it right.
Im actually quite confused and not too sure of what im doing wrong here. Sorry.
And yes I have been working in VC++ and now Code::Blocks trying to debug my way through this.
Call me Wice, Miami Wice!
Just include the header file for the SIngleton whereever you want to use it and then use it:
Easy really!
Code: Select all
CCore::Instance()->SomeFunction();
Tried your method and so far everything seems ok but it wont compile all the way since it gets an error saying:I've gotten this error before while working with the singleton earlier on so my guess would be it has something to do with the design of the singleton.. am i far off here?
btw: I've updated the source files at http://www.zoneone.dk/PubDebug/
Code: Select all
CCore.obj : error LNK2001: unresolved external symbol "private: __thiscall CCore::CCore(void)" (??0CCore@@AAE@XZ)
btw: I've updated the source files at http://www.zoneone.dk/PubDebug/
Call me Wice, Miami Wice!
Hi,Saku wrote:Code: Select all
CCore.obj : error LNK2001: [b]unresolved external symbol "private: __thiscall CCore::CCore(void)"[/b] (??0CCore@@AAE@XZ)
Your constructor for CCore is declared in the header (prototype) but has no body in the CCore.cpp.
There is no
CCore::Core() {
}
Eureka! (what a nice word )
That sure did the trick. Everything is back to normal but with the singleton pattern applied Now I can start focusing on some of the more fun part of the programming
Thank you both of you!
For interrested readers I will upload the latest files in the Public Debug (http://www.zoneone.dk/PubDebug/) in short time.
/Saku
That sure did the trick. Everything is back to normal but with the singleton pattern applied Now I can start focusing on some of the more fun part of the programming
Thank you both of you!
For interrested readers I will upload the latest files in the Public Debug (http://www.zoneone.dk/PubDebug/) in short time.
/Saku
Call me Wice, Miami Wice!