我编写了一个模板函数来比较两个变量:
template <class t>
int compare(const t &a, const t &b) {
if(a>b) return 1;
if (a<b) return -1;
return 0;
}
int main(int argc, const char *argv[])
{
cout << compare("hi","world");
return 0;
}
我得到以下错误
../src/templates.cpp: In function ‘int main(int, const char**)’:
../src/templates.cpp:11: error: no matching function for call to ‘compare(const char [3], const char [6])
Please explain.
Also if I write cout << compare("hi", "wo");
it compiles properly.
Or if I remove the &
and declare the function like int compare(const t a, const t b)
it compiles.