【发布时间】:2013-09-26 21:09:37
【问题描述】:
#include <iostream>
using namespace std;
template <class MyType>
MyType GetMax (MyType a, MyType b, MyType c) {
return std::max(std::max(a, b), c);
}
int main () {
int a = 5, b = 6, c = 7, d;
char e [] = "1", f [] = "2", g [] = "3", h;
d=GetMax<int>(a,b,c);
cout << d << endl;
}
错误:没有匹配函数调用 'GetMax(char [2], char [2], char [2])'
该程序在 int 上运行良好,但我不确定 char 的问题是什么
【问题讨论】:
-
删除
GetMax<int>(a,b,c)中的<int>,让它为你推断类型。此外,如果您将它与char*s 一起使用,它将比较指针而不是内容。如果您想比较字符串的内容,请使用std::string(或修改您的 func 以获取比较器) -
相关(因为
std::max不喜欢我的char与int比较):stackoverflow.com/questions/2304732/…
标签: c++