Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / Trying to use static methods/members, need some help

Author
Message
draknir_
18
Years of Service
User Offline
Joined: 19th Oct 2006
Location: Netherlands
Posted: 11th Aug 2010 07:41
Hi guys,

I've been spoilt with c# coding the last few years and now I'm back onto c++ and finding that I'm having trouble with stuff that is supposed to be simple.

Heres my code:

System.h


System.cpp


The idea here is to have a map which maps strings against integer values, to access images with a string instead of hardcoded values. This class isn't done, so it doesn't handle anything like unloading images. I want to access the image methods without passing an instance of System, so I used static.

Now i get this error:


If I change the map to static, I get this linker error:


Can any of you bright chaps help me out?

cheers,
Draknir
dark coder
22
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 11th Aug 2010 08:50
Static methods can only access static members, unless you dereference an instance of a class, otherwise it'd a bit like asking why something like this doesn't work: class A{public: int test;}; int main(){ test = 5; }

If you make your map static then you must actually define it somewhere, this definition should be in one of your .cpp files, for example: map<string, int> System::m_images;

I personally find this a bit annoying to do, so I prefer to use the singleton design pattern, this page seems to have good info on it: http://www.codeguru.com/forum/showthread.php?t=344782

draknir_
18
Years of Service
User Offline
Joined: 19th Oct 2006
Location: Netherlands
Posted: 11th Aug 2010 20:00
Thanks dark coder, defining it fixed up my error. I realize it was stupid to ask why a static method can't access a static member, I was just frustrated it wasn't working when I had them both static

As for singletons, that sounds logically right, but doesn't it mean you need to pass instances around anyways?
Zotoaster
19
Years of Service
User Offline
Joined: 20th Dec 2004
Location: Scotland
Posted: 11th Aug 2010 20:47
Quote: "As for singletons, that sounds logically right, but doesn't it mean you need to pass instances around anyways?"


Nope. Usually you simply have something along the lines of System::GetInstance().LoadImage(), which can be done from anywhere as GetInstance() would be static.

"everyone forgets a semi-colon sometimes." - Phaelax
draknir_
18
Years of Service
User Offline
Joined: 19th Oct 2006
Location: Netherlands
Posted: 11th Aug 2010 22:32 Edited at: 11th Aug 2010 22:32
Thanks Zotoaster

After doing some googling I decided on a template singleton from this site: http://www.yolinux.com/TUTORIALS/C++Singleton.html

Works and looks great.

Login to post a reply

Server time is: 2024-11-19 20:16:50
Your offset time is: 2024-11-19 20:16:50