[Solved] Creating a Function

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
Ein
Posts: 33
Joined: Thu Feb 12, 2009 11:39 pm
Location: UK - Matrix
Contact:

[Solved] Creating a Function

Post by Ein »

I have been trying to create a fucntion that gets the time, only when I create it I cant make it see that the device has been created >_< you know what I mean something like public, protected and private.

Code: Select all

      void SetTimer();
      
      void SetTimer(){
           u32 Time = device->getTimer()->getTime();     
      };
This come up with an error "...\fullGame02.cpp `device' undeclared (first use this function)" I am sure that it is something simple.

Thanks,
Pete
Last edited by Ein on Tue Feb 17, 2009 5:06 pm, edited 1 time in total.
Ein knowledge seeker
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Ehh, yeah, C++ Primer, second chapter (the one after introduction...). You cannot use things from other classes without getting a handle to it. Assuming that device is not a global variable, how should SetTimer know where to find your "device"? Either give it a device parameter or use some other means to tell it to that function.
loki1985
Posts: 214
Joined: Thu Mar 31, 2005 2:36 pm

Post by loki1985 »

well, it means the compiler cannot find the "device" object at the specific location.

http://en.wikipedia.org/wiki/Scope_(programming)

/EDIT: too late...
Ein
Posts: 33
Joined: Thu Feb 12, 2009 11:39 pm
Location: UK - Matrix
Contact:

Post by Ein »

hahah I got it lol, I don't know why I dident think of this before

Code: Select all

//header 
//Create device
//create time function

//create main function
^_^ thanks for your help.
Ein knowledge seeker
arras
Posts: 1622
Joined: Mon Apr 05, 2004 8:35 am
Location: Slovakia
Contact:

Post by arras »

You should not create device outside the main(). But you can declare it:

Code: Select all

IrrlichtDevice *Device;

void yourFunction()
{
   Device->getWhatEverYouWant();
}

int main()
{
   Device = createDevice(...);

   ...rest of code...
}
In this case you Device is global. If you do not like it (some people don't for some strange reason) you should do:

Code: Select all

void yourFunction(IrrlichtDevice *device)
{
   device->getWhatEverYouWant();
}
That is, you pass device to your function as parameter when you call that function.
loki1985
Posts: 214
Joined: Thu Mar 31, 2005 2:36 pm

Post by loki1985 »

arras wrote:If you do not like it (some people don't for some strange reason)
whats strange about this? it is simply bad style of coding to use globals when not necessary. if working on a big (e.g. commercial) project which uses a lot of globals, it is easy to see why that is a bad idea in most cases.
Ein
Posts: 33
Joined: Thu Feb 12, 2009 11:39 pm
Location: UK - Matrix
Contact:

Post by Ein »

Code: Select all

void yourFunction(IrrlichtDevice *device)
{
   device->getWhatEverYouWant();
}
If I were to use the above code, does this tell the function that the class (device) is located at this memory address?
Ein knowledge seeker
Moo
Posts: 30
Joined: Fri Jan 09, 2009 10:07 pm

Post by Moo »

Ein wrote:

Code: Select all

void yourFunction(IrrlichtDevice *device)
{
   device->getWhatEverYouWant();
}
If I were to use the above code, does this tell the function that the class (device) is located at this memory address?
Yes. You could read a bit on pointers. I also recommend reading a similar question and an excellent article from GameDev.

You could also try a more general C++ tutorial. Good luck!
rogerborg wrote:Answering a noob question is like having sex with a midget: there's a wonderfully satisfying sensation of being a giant.
Ein
Posts: 33
Joined: Thu Feb 12, 2009 11:39 pm
Location: UK - Matrix
Contact:

Post by Ein »

I shall read them now, thank you for this ^_^
Ein knowledge seeker
arras
Posts: 1622
Joined: Mon Apr 05, 2004 8:35 am
Location: Slovakia
Contact:

Post by arras »

loki1985 wrote:whats strange about this? it is simply bad style of coding to use globals when not necessary.
There is nothing bad with globals, they are standard part of C++. Bad can be only way you use them. But that would be your fault not fault of bad globals. Just anything can be used in a wrong way but that is not reason to call it bad or tell others to not use it.
Post Reply