没有函数模板的实例"max"与参数列表参数类型匹配(int、int)
No instance of function template "max" matches the argument list argument types are (int, int)
我刚刚开始使用c ++,我对模板没有很多了解,我做了一个模板函数,我在Visual Studio中收到此错误:
函数模板"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;
}
复制错误在 cout 的最大值中发现
谢谢!
非const
引用参数必须由实际变量支持(松散地说(。所以这将起作用:
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;
}
但是,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;
}
您的函数需要两个 l 值引用,但是,您要传递的是两个 r 值。
传递两个变量或更改函数签名以接受 r 值引用。
相关文章:
- 类型为 "int*" 的参数与 C++ 中错误类型"int**"参数不兼容
- 为表示一个或多个操作的C++函数的int参数寻找类型安全的替换
- 为什么SDL_QueryTexture不为其 int* 参数赋值?
- 如何增加模板类型 int 参数?
- 如何将INT参数放入C 中的Sigleton构造器中
- 如何使用 int 参数循环访问模板函数
- 是否可以使用C 函数中的const int参数创建数组
- 在与时间一起使用 srand 时,如果我没有time_t参数明确地将参数转换为无符号的 int 参数,这有关系吗?
- 带有unsigned int参数的C++函数在使用negative调用它时会得到奇怪的结果
- 是一个与int[]参数完全相同的int*参数
- 我们可以将所有"int"参数和返回类型替换为 DllImport 方法签名中的"IntPtr"吗?
- 字符串和int参数出现C++编译器错误
- 模板int参数来自C++11中的常量表达式
- 避免使用int参数的模板类的C++爆炸性实例化
- 我们可以创建临时传入的"std::vector<int>"参数吗?
- 如何使用QT5中的lambdas将具有int参数的信号连接到具有枚举参数with的插槽
- 将指向函数的指针数组作为"int"参数传递是否可以将地址转换为整数?
- 为什么系统在没有警告的情况下接受作为int参数传递的long int
- 使用 int&& 参数通过 int&& 进行函数初始化
- 成员函数的Boost线程int参数不工作