Default Arguments in functions and extern
Moderator: Coders of Rage
Default Arguments in functions and extern
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).
- programmerinprogress
- Chaos Rift Devotee
- Posts: 632
- Joined: Wed Oct 29, 2008 7:31 am
- Current Project: some crazy stuff, i'll tell soon :-)
- Favorite Gaming Platforms: PC
- Programming Language of Choice: C++!
- Location: The UK
- Contact:
Re: Default Arguments in functions and extern
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?
- 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?
---------------------------------------------------------------------------------------
I think I can program pretty well, it's my compiler that needs convincing!
---------------------------------------------------------------------------------------
And now a joke to lighten to mood :D
I wander what programming language anakin skywalker used to program C3-PO's AI back on tatooine? my guess is Jawa :P
I think I can program pretty well, it's my compiler that needs convincing!
---------------------------------------------------------------------------------------
And now a joke to lighten to mood :D
I wander what programming language anakin skywalker used to program C3-PO's AI back on tatooine? my guess is Jawa :P
- hurstshifter
- ES Beta Backer
- Posts: 713
- Joined: Mon Jun 08, 2009 8:33 pm
- Favorite Gaming Platforms: SNES
- Programming Language of Choice: C/++
- Location: Boston, MA
- Contact:
Re: Default Arguments in functions and extern
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.programmerinprogress wrote:
- if you want to assign default values, ONLY assign once, preferably in the prototype, not the final definition
"Time is an illusion. Lunchtime, doubly so."
http://www.thenerdnight.com
http://www.thenerdnight.com