我正在尝试做的事情:
编写上一个练习中的模板的专用版本来处理vector<const char*>
和一个程序
使用这个专业。
我这样写程序:
template<typename T>
int count(vector<T> tvec, const T &t);
template<>
int count(vector<const char *> tvec, const char *const &s)
{
int count = 0;
for (auto c : tvec)
if (c == s) {
++count;
}
return count;
}
template<typename T>
int count(vector<T> tvec, const T &t)
{
int count = 0;
for (auto c : tvec)
if (c == t) {
++count;
}
return count;
}
cout << count(svec, "GUO");
但我收到错误消息
deduced conflicting types for parameter ‘const T’ (‘std::basic_string<char>’ and ‘char [4]’)
我想知道如何处理这个问题。而且,在模板函数中,似乎可以将数组更改为指针,为什么我的程序无法处理它?
不要对两个参数进行推断,这会导致冲突。写下这个:
template <typename T>
int count(const vector<T>& tvec, const typename vector<T>::value_type& t);
另外,考虑重载而不是专门化。专门化一个函数模板几乎不是您想要的。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)