Reputation: 97
In my ".h" file , I have defined :
example(CQueue* queue = NULL, double code = 0);
In 2 different methods of my ".cpp" file, I have to use :
example(queue);
example(code2);
I get errors in both cases. I didnt want to overload. Is defining it :
example(CQueue* queue, double code = 0);
the only way? or can I define it the way it is currently defined?
Upvotes: 0
Views: 69
Reputation: 59617
If you want to pass a specific value for code
, you must also pass a value for the earlier optional parameters, even if they're given default values in the method signature.
Just explicitly pass what you've defined as the default value: example(NULL, code2);
Your definition is fine, but if you want to be able to also call example(code2)
then you must overload.
example(queue);
should work depending on the declaration of queue
.
Upvotes: 1
Reputation: 258618
You can define it like that, but don't expect the second call to work. The first one should be fine, provided queue
is a CQueue*
. If it's an object, you'll need to pass its address: &queue
.
You'll have to change the second call to:
example(NULL, code2);
or, as you said, overload.
Upvotes: 2