Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
“没有函数模板的实例”;“最大值”;匹配参数列表参数类型为(int,int) 我刚开始用C++,我没有很多关于模板的知识,我制作了一个模板函数,我在VisualStudio:_C++_Templates_Arguments - Fatal编程技术网

“没有函数模板的实例”;“最大值”;匹配参数列表参数类型为(int,int) 我刚开始用C++,我没有很多关于模板的知识,我制作了一个模板函数,我在VisualStudio:

“没有函数模板的实例”;“最大值”;匹配参数列表参数类型为(int,int) 我刚开始用C++,我没有很多关于模板的知识,我制作了一个模板函数,我在VisualStudio:,c++,templates,arguments,C++,Templates,Arguments,//没有函数模板“max”的实例与参数列表匹配参数类型为(int,int) //C2664'T max(T&,T&)':无法将参数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

//没有函数模板“max”的实例与参数列表匹配参数类型为(int,int) //C2664'T max(T&,T&)':无法将参数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 and 55 is " << max(34, 55) << endl;
}
#包括“stdafx.h”
#包括
使用名称空间std;
模板
T最大值(T&t1、T&t2)
{
返回t1cout您的函数需要两个l值引用,但是,您要传递的是两个r值


传递两个变量或更改函数签名以接受r值引用。

非常量引用参数必须由实际变量(粗略地说)支持。因此这将起作用:

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;
}

使用namespace std;
删除
,并按值传递参数。发布有关生成错误的问题时,请将实际错误完整复制粘贴到问题正文中。当然,它应该包括可能的信息注释。当然,此代码处于sin:
使用namespac的状态e std;
意味着您可能在全局命名空间中拥有标准库的
std::max
定义以及您自己的
max
定义。这不是导致此特定问题的原因,但它最终会咬到您。或者
const
左值引用。
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;
}