错误消息“推导出参数”const T“的冲突类型” [英] Error message "deduced conflicting types for parameter 'const T'"

查看:134
本文介绍了错误消息“推导出参数”const T“的冲突类型”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要做什么:


编写上一个练习中的模板的特殊版本来处理 vector< const char *> 和一个程序,
使用这个特化。

Write a specialized version of the template from the previous exercise to handle vector<const char*> and a program that uses this specialization.

我写了这样的程序:

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");

但我得到的错误是

推断参数'const T'('std :: basic_string< char>'和'char [4]')的冲突类型

我想知道如何处理这个。并且在模板函数中,看起来数组可以改变为指针,为什么我的程序不能处理它?<​​/ p>

I want to know how to handle this. and further, in the template function, it seems that an array can be changed to the pointer, why my program cannot handle it?

推荐答案

p>不要在两个参数上推断,它会导致冲突。写这个:

Don't deduce on both parameters, it leads to conflicts. Write this:

template <typename T>
int count(const vector<T>& tvec, const typename vector<T>::value_type& t);

此外,考虑重载而不是专业化。专业化功能模板几乎从不是你想要的。

Also, consider overloading instead of specializing. Specializing a function template is pretty much never what you want.

这篇关于错误消息“推导出参数”const T“的冲突类型”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆