Page 1 of 1

Default Arguments in functions and extern

Posted: Thu Jun 11, 2009 8:37 pm
by herby490
I'm making a simple game that will have multiple source files. I would like to have a default argument in one of my functions however if I declare it extern in another file and compile it I get an error saying that I am not allowed to reassign a value to it. If I remove the default value from the parameters in the extern file and compile it I get a different error saying that when I use it I have I am missing a parameter (I left it blank since the parameter is NULL).

Re: Default Arguments in functions and extern

Posted: Fri Jun 12, 2009 7:34 am
by programmerinprogress
Well, while I don't understand what exactly your problem is, I can offer a few tips when it comes to declaring prototypes in a separate file and setting default values etc.

- if you want to assign default values, ONLY assign once, preferably in the prototype, not the final definition
- if the answer is NULL, you still have to assign it if it's going to be a default argument, the compiler has to know that you intend to make it default, so it needs to be aware that you might "miss" the argument out
- default arguments start from right to left, you have to be consistent, you can't just say that you want your first argument to be default, and the rest to not be not

if follow these steps, then your function declarations should be valid, at least at single-file scope, i'll have to consult my big book to read up on the extern keyword, but from memory I understand its about allowing variables access to other files, right?, basically the opposite of the non-class use of the keyword static?

Re: Default Arguments in functions and extern

Posted: Fri Jun 12, 2009 8:50 am
by hurstshifter
programmerinprogress wrote:
- if you want to assign default values, ONLY assign once, preferably in the prototype, not the final definition
Agreed. I've ran into this problem in the past and it took me a while to figure it out (probably should have just asked). You probably had the extern initialized in the header and cpp file. Whenever I use an external I've trained myself to initialize it in the prototype, just to keep consistency with future projects.