上級 › error: no matching function for call to '...'(テンプレートの型推論失敗)の原因と直し方 › パターン1
error: no matching function for call to '...'(テンプレートの型推論失敗)の原因と直し方
テンプレート関数の2つの引数に異なる型を渡したときに出るエラーです。
1 #include <iostream> 2 template <typename T> 3 T add(T a, T b) { 4 return a + b; 5 } 6 int main() { 7 std::cout << add(3, ___) << std::endl; ^ 8 return 0; 9 }
main.cpp:7:21: error: no matching function for call to 'add(int, const char [2])'
空欄を埋めてコンパイル・実行を通す
同じ型パラメータTを2箇所で使うテンプレートは、両方の引数から矛盾なく同じ型が推論できないとコンパイルできません。3はint、"5"はconst char*で型が異なるため推論に失敗します。
次の問題