Code Fix

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

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

テンプレート関数の2つの引数に異なる型を渡したときに出るエラーです。

 1  #include <iostream>
 2  template <typename T>
 3  T multiply(T a, T b) {
 4      return a * b;
 5  }
 6  int main() {
 7      std::cout << multiply(4, ___) << std::endl;
                                  ^
 8      return 0;
 9  }
main.cpp:7:26: error: no matching function for call to 'multiply(int, char)'

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

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

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

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

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

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