1

I have this function I defined here;

static double round(double x, int d = 0) {
    return (x * pow(10.0, d) - fmod(x * pow(10.0, d) - 0.5, 1.0) + 0.5) / pow(10.0, d);
}

And I have this prototype defined above the main method;

static double round(double x, int d = 0);

But if I try to call the method from within the main method with only one argument, it gives me the above syntax error. I have no idea why this is happening and it's driving me mad.

3
  • Can you post the 'above syntax error'? Also, where is the function definition written? Commented Jan 20, 2015 at 17:04
  • "Above syntax error", ie. the one in the title. Commented Jan 20, 2015 at 17:06
  • @Maurdekye Your title doesn't (and shouldn't) describe an exact syntax error. Commented Jan 20, 2015 at 17:20

4 Answers 4

3

If you're including any standard headers, then it's probably conflicting with the C library function of the same name.

Unfortunately, C++ library implementations are allowed to dump these in the global namespace as well as namespace std, so it's difficult to avoid such conflicts.

The only portable solution is to change your function so it has a different name, or different (non-optional) parameters, or is in a different namespace, to the C library function.

(Also, as other answers point out, you can only specify the default arguments once. You should remove them from the definition, leaving them just in the declaration.)

Sign up to request clarification or add additional context in comments.

Comments

3

Remove the default value d = 0 from the definition, it has already been declared, i.e

static double round(double x, int d = 0);
// .. 
static double round(double x, int d) {
    return (x * pow(10.0, d) - fmod(x * pow(10.0, d) - 0.5, 1.0) + 0.5) / pow(10.0, d);
}

EDIT:

Your error is due to an ambiguous call of function round(...) when only one argument is passed to this function, the compiler cannot guess if you are calling your function or the std implementation of round. To avoid this ambiguous call, (a) put your function into your own namespace (b) think of another name for your function. I would go with the (b) option.

3 Comments

Yep. Still get the error. I removed it from the definition, and kept it in the prototype.
Most probably in his code round() is declared in the global namespace and that's causing a conflict with the standard round() function
(b) won't work (at least not portably), since some implementations dump the C library into the global namespace whether you want them to or not. Unfortunately, the standard allows them to do that.
0

A default value can only be specified once. When you have a separate prototype and definition for a function with default arguments, you need to put the default value only on the prototype, not the definition. Take the "=0" out of the definition (but leave it in the prototype) and it should work.

Comments

0

C++ does not support redefinition of default argument. You should remove the definition of default argument from the definition of the function like this:

static double round(double x, int d/* = 0*/);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.