Code Fix

上級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])'

空欄を埋めてコンパイル・実行を通す

広告
広告スロット(未設定)

error: no matching function for call to '...'(テンプレートの型推論失敗)の原因と直し方

同じ型パラメータTを2箇所で使うテンプレートは、呼び出し時にすべての箇所で矛盾なく同じ型が推論できなければコンパイルできません。

このエラーの解説と類題をまとめて見る →

広告
広告スロット(未設定)