开发者

no matching function error [templates]

开发者 https://www.devze.com 2023-01-25 10:05 出处:网络
Why doesn\'t the following code compile? template <class T> void foo_bar(T =5 , T = 10 , T = 15) {

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 开发者_Python百科fix the error?


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号