Reputation: 33
Why doesn't the following code compile?
template <class T>
void foo_bar(T =5 , T = 10 , T = 15)
{
}
int main()
{
foo_bar();
}
I get this error no matching function for call to 'foo_bar()'
. How to fix the error?
Upvotes: 3
Views: 196
Reputation: 6254
The compiler has to be able to figure out types, but if you never give it any, it doesn't know what to do with those default values. Basically, it couldn't match your call to any function, because you didn't give it a type to work with and the function definition didn't help (for the aforementioned reasons). So you'll need to add a type in brackets to the call, like
int main()
{
foo_bar<int>();
return 0;
}
or whatever type strikes your fancy.
Upvotes: 1
Reputation: 92924
The type of default arguments in a function cannot help in deducing the template type-parameter
. T
cannot be deduced in any case when you call the function like foo_bar()
and so you get the error.
So try this foo_bar<int>();
. In this case there won't be any such problem because the type of T
is explicitly specified.
Upvotes: 3