Help with singleton needed!

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
Saku
Posts: 158
Joined: Wed Jan 05, 2005 8:48 am
Location: Denmark

Help with singleton needed!

Post by Saku »

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
Call me Wice, Miami Wice!
Fred

Post by Fred »

What problem are you having?
Saku
Posts: 158
Joined: Wed Jan 05, 2005 8:48 am
Location: Denmark

Post by Saku »

:lol: 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.
Call me Wice, Miami Wice!
trekker
Posts: 18
Joined: Sat Apr 09, 2005 6:40 pm

Post by trekker »

Saku wrote: So if it's posible I would appreciate if you'd check if the fundamentals of my singleton is correct.
hmm, your code seems to work for me, not sure what could be wrong

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();

}
Fred

Post by Fred »

Have you run it through the debugger yet?
Saku
Posts: 158
Joined: Wed Jan 05, 2005 8:48 am
Location: Denmark

Post by Saku »

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.
Call me Wice, Miami Wice!
Fred

Post by Fred »

Just include the header file for the SIngleton whereever you want to use it and then use it:

Code: Select all

CCore::Instance()->SomeFunction();
Easy really!
Saku
Posts: 158
Joined: Wed Jan 05, 2005 8:48 am
Location: Denmark

Post by Saku »

Tried your method and so far everything seems ok but it wont compile all the way since it gets an error saying:

Code: Select all

CCore.obj : error LNK2001: unresolved external symbol "private: __thiscall CCore::CCore(void)" (??0CCore@@AAE@XZ)
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/
Call me Wice, Miami Wice!
trekker
Posts: 18
Joined: Sat Apr 09, 2005 6:40 pm

Post by trekker »

Saku wrote:

Code: Select all

CCore.obj : error LNK2001: [b]unresolved external symbol "private: __thiscall CCore::CCore(void)"[/b] (??0CCore@@AAE@XZ)
Hi,

Your constructor for CCore is declared in the header (prototype) but has no body in the CCore.cpp.

There is no

CCore::Core() {
}
Saku
Posts: 158
Joined: Wed Jan 05, 2005 8:48 am
Location: Denmark

Post by Saku »

Eureka! (what a nice word :lol: )
That sure did the trick. Everything is back to normal but with the singleton pattern applied 8) Now I can start focusing on some of the more fun part of the programming :D
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!
Fred

Post by Fred »

Glad you got it working. It isn't so difficult now is it? :)
Saku
Posts: 158
Joined: Wed Jan 05, 2005 8:48 am
Location: Denmark

Post by Saku »

Nope not really ;)
It's just that I'm used to program small utilities in Visual Basic so classes, pointers and design patterns are totally new to me. This is like a whole new world to me :)
Call me Wice, Miami Wice!
Fred

Post by Fred »

I've been programming for 20 years and i'm still learning something new every week. :D
Post Reply