c++ - No instance of function template "max" matches the argument list argument types are (int, int) -
i starting c++ , don't have lot of knowledge templates, made template function , recieving error in visual studio:
//no instance of function template "max" matches argument list argument types (int, int) //c2664't max(t &,t &)': cannot convert argument 1 'int' 'int &'
#include "stdafx.h" #include <iostream> using namespace std; template <class t> t max(t& t1, t& t2) { return t1 < t2 ? t2 : t1; } int main() { cout << "the max of 34 , 55 " << max(34, 55) << endl; }
the copiling error found in max of cout
thank you!
a non-const
reference parameter must backed actual variable (loosely speaking). work:
template <class t> t max(t& t1, t& t2) { return t1 < t2 ? t2 : t1; } int main() { int = 34, j = 55; cout << "the max of 34 , 55 " << max(i, j) << endl; }
however, const
reference parameter not have requirement. want:
template <class t> t max(const t& t1, const t& t2) { return t1 < t2 ? t2 : t1; } int main() { cout << "the max of 34 , 55 " << max(34, 55) << endl; }
Comments
Post a Comment