#include< iostream >
using namespace::std;
int foo(); //function with no argument
int foo(int=10); //Default argument
int foo()
{
cout<<"\n Foo , I am foo \n";
return 0;
}
int foo(int f)
{
cout<<"\n I am Foo too.\n";
return 0;
}
int main()
{
foo();
foo(2);
cin.get();
return 0;
}

- 15,016
- 11
- 93
- 186

- 11
- 4
-
1Get rid of the default argument. – drescherjm Oct 27 '18 at 14:10
-
5What's the point of having a no-argument overload when there is one with a default argument? If it's a whole different function, it should have a different name. – DeiDei Oct 27 '18 at 14:11
-
I want to know that is there any way to call foo() ? – Udesh Oct 27 '18 at 14:14
-
1Fixing your code is trivial. Remove the `=10`. – Jesper Juhl Oct 27 '18 at 14:14
-
1Function overloading is about being able to call functions with the same name based on their argument lists. Having two functions with the same name and identical argument lists just doesn't fit into overloading. You've gotten answers that show how you can do it; it's much simpler to give them different names. In other words, replace `static_cast
(foo)()` with `bar()`. – Pete Becker Oct 27 '18 at 14:21 -
are you looking for a function with a variable list of arguments? see https://stackoverflow.com/questions/1657883/variable-number-of-arguments-in-c – Wolfgang Fahl Oct 27 '18 at 14:22
-
The essential question is ***do the two functions essentially do the same thing***? If so, you should not put a default argument in the second declaration. If they do different things, you need to rename one, or give us a good reason why they have to have the same name. – Spencer Oct 27 '18 at 14:33
3 Answers
There are no clean ways of doing this, only ugly ones. Such as:
static_cast<int (*)()>(&foo)();
Or:
int (*p)() = &foo;
(*p)();
The bottom line is that you cannot rely on overload resolution, because of ambiguity. Hence you need to explicitly resolve the symbol unambiguously, using this kind of an approach.
The preferred solution is to avoid having ambiguous overloads in the first place. It's usually possible to find some reasonable way to do that, but the manner of that depends on the actual details.

- 114,536
- 5
- 94
- 148
You can explicitly cast a function pointer, not sure if you should though.
#include <iostream>
using namespace::std;
int foo(); //function with no argument
int foo(int=10); //Default argument
int foo()
{
cout<<"\n Foo , I am foo \n";
return 0;
}
int foo(int f)
{
cout<<"\n I am Foo too.\n";
return 0;
}
int main()
{
static_cast<int(*)()>(foo)();
foo(2);
cin.get();
return 0;
}
edit:
This line static_cast<int(*)()>(foo)();
is just a shortened form of a cast and a call to the function.
To explain it we can pull it apart in 3 steps:
Define myFuncType to be a pointer to a function that returns an int and takes no arguments
using myFuncType = int(*)();
Resolve the overloaded name foo by casting it to the function type that takes no arguments and store it in a variable named f
auto f = static_cast<myFuncType>(foo);
// or auto f = (myFuncType)foo;
// or auto f = (myFuncType)&foo; C++ actually does the address-of implicitly with functions
Last one is calling the function pointer
f(); //or (*f)(); C++ automatically dereferences function pointer on call, but you can also manually dereference

- 7,981
- 1
- 26
- 34
- Remove the default argument, or
- Rename one of the functions
It's not clear why the two function declarations use the same name. You should only do that if they're supposed to "do the same thing".
Remove the default argument
If the two functions are supposed to do the same thing, then:
When you specify a default argument to a function, you get an extra function signature thrown in. Thus,
int foo(int arg=10);
also gives you
int foo();
It says that with *any call"" to foo()
the value of arg is 10
.
But then you've gone and thrown in an extra int foo();
that returns 0
, confusing the compiler. On a more abstract level, you've actually confused yourself as to what the default argument is for the "thing" your function is supposed to do.
Rename one of the functions
If the two functions dont do the same thing they shouldn't have the same name. There are a few different ways to do this:
- Make up a new name for one
- put one of them in a namespace
- use a template

- 1,924
- 15
- 27