Page 1 of 1

[Solved] Creating a Function

Posted: Tue Feb 17, 2009 4:15 pm
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

Posted: Tue Feb 17, 2009 4:19 pm
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.

Posted: Tue Feb 17, 2009 4:19 pm
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...

Posted: Tue Feb 17, 2009 5:00 pm
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.

Posted: Tue Feb 17, 2009 6:10 pm
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.

Posted: Tue Feb 17, 2009 6:49 pm
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.

Posted: Tue Feb 17, 2009 8:36 pm
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?

Posted: Tue Feb 17, 2009 8:45 pm
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!

Posted: Tue Feb 17, 2009 8:57 pm
by Ein
I shall read them now, thank you for this ^_^

Posted: Tue Feb 17, 2009 9:32 pm
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.