调用专用模板时出现错误“没有匹配的函数来调用[...]”

时间:2018-11-03 22:26:23

标签: c++ string templates template-specialization

我有许多专门的模板,如下所示:

template <unsigned long long>
Result<unsigned long long> strToNumber (const std::string& str)
{
    std::string cleanStr;

    //processing 'str' here...

    return strtoull(cleanStr.c_str(), NULL, 10);
}

当我使用调用此函数时

    auto idResult = Util::str::strToNumber<unsigned long long>(std::string(idFromDB["id"].GetString()));

我收到以下错误:

RequestManager.cpp:30:128: error: no matching function for call to ‘strToNumber(std::__cxx11::string)’
 igned long long> idResult = Util::str::strToNumber<unsigned long long>(std::string(dataFromDB["id"].GetString()));

我在做什么错了?

1 个答案:

答案 0 :(得分:0)

您似乎要写的是这样的一行:

redis.zrevrange('myleaderboard', startIndex, endIndex)

但是由于#include <string> template <typename Result> Result strToNumber (const std::string& str) { std::string cleanStr; //processing 'str' here... return std::strtoull(cleanStr.c_str(), nullptr, 10); } int main(int argc, char *argv[]) { auto idResult = strToNumber<unsigned long long>("123456789"); return 0; } 返回了std::strtoull(),因此如果有人调用unsigned long long,该函数的模板形式可能会导致问题。

因此,这表明您的代码应类似于:

strToNumber<int>("-1")

但是,如果您真的想使用静态多态代码的灵活性,可以看看:

#include <string>

unsigned long long strToNumber (const std::string& str)
{
    std::string cleanStr;

    //processing 'str' here...

    return std::strtoull(cleanStr.c_str(), nullptr, 10);
}

int main(int argc, char *argv[])
{
    auto idResult = strToNumber("123456789");
    return 0;
}