No instance of function template "max" matches the argument list argument types are (int, int)

polmonroig

I am just starting with c++ and I don't have a lot of knowledge about templates, I made a template function and I am recieving this error in Visual Studio:

//No instance of function template "max" matches the argument list argument types are (int, int) //C2664'T max(T &,T &)': cannot convert argument 1 from 'int' to '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 and 55 is " << max(34, 55) << endl;
}

The copiling error is found in the max of the cout

Thank you!

Arthur Tacca

A non-const reference parameter must be backed by an actual variable (loosely speaking). So this would work:

template <class T>
T max(T& t1, T& t2)
{
    return t1 < t2 ? t2 : t1;
}
int main()
{
int i = 34, j = 55;
cout << "The Max of 34 and 55 is " << max(i, j) << endl;
}

However, a const reference parameter does not have this requirement. This is probably what you want:

template <class T>
T max(const T& t1, const T& t2)
{
    return t1 < t2 ? t2 : t1;
}
int main()
{
cout << "The Max of 34 and 55 is " << max(34, 55) << endl;
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

No instance of function template matches the argument list, argument types are: (std::string, CommandLineArgumentTypes)

CUDA template error: no instance of function template matches the argument list

How to fix "no instance of overloaded function "getline" matches the argument list" when using getline with an int

No instance of function template "ExtractSpherical_kernel" matches the argument list

no instance of function template matches the argument list (trying to print array)

C++ no instance of function template matches the argument list

No instance of function template "std::make_pair" matches the argument list

no instance of function template matches the argument list i dont know why

OnComponentBeginOverlap.AddDynamic says no instance of the function template matches the argument list?

No instance of overloaded function matches the argument list

Template function with template argument parametrized over int

c++ template - More than one instance of overloaded function matches the argument list

How can I fix a "no instance of function template 'Defer' matches the argument list" error in C++?

C++ template class: No instance of constructor matches the argument list

no instance of constructor matches the argument list

no instance of "getline" matches the argument list

Error No instance of overloaded function "getline" matches the argument list

no instance of overloaded function "std::make_unique" matches the argument list

A function with a template in its argument throws an error when called with an int

no instance of constructor "AcademicStaff::AcademicStaff" matches the argument list

No function matches the given name and argument types

Error passing Eigen matrix to a function in C++: "no instance of overloaded function matches the argument list"

Argument types problem ('float', 'const int') in array

lambda function doesn't matches argument list

TypeError Exception: argument must be int or float on a view argument inside a template

Variadic template function with equal argument types

Template argument deduction when types are not in function prototype

More than one instance of overloaded function matches the argument list and I can't find where the error happens

C++ No instance of overloaded function matches the argument list when calling std::replace()