简体   繁体   中英

No matching function for call to a template

I asked a question here and got an excellent response from the community:A templated 'strdup()'?

But now, when I do the following:

struct CurlInfo {
    long response_code;
    std::string effective_url;
};

CurlInfo info;
CURL *curl = curlInit(url, "", "");
if (curl != nullptr) {
    long rescode;
    curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &rescode);
}

info.response_code = gk::anydup(rescode, sizeof(long));

With the following template:

template<typename T>
std::vector<T> anydup(std::vector<T> &src, size_t len) {
    std::vector<T> v(len);
    std::copy(src.cbegin(), src.cend(), v);
    return v;
}

I get the following:

/home/phobos/Programming/FyreDL/src/cmnroutines.cpp:89:66: error: no matching function for call to 'anydup(long int&, long unsigned int)'
             info.response_code = gk::anydup(rescode, sizeof(long));
                                                              ^
In file included from /home/phobos/Programming/FyreDL/src/cmnroutines.cpp:43:0:
/home/phobos/Programming/FyreDL/src/cmnroutines.hpp:89:16: note: candidate: template<class T> std::vector<T> gk::anydup(std::vector<T>&, size_t)
std::vector<T> anydup(std::vector<T> &src, size_t len) {
            ^~~~~~
/home/phobos/Programming/FyreDL/src/cmnroutines.hpp:89:16: note:   template argument deduction/substitution failed:
/home/phobos/Programming/FyreDL/src/cmnroutines.cpp:89:66: note:   mismatched types 'std::vector<T>' and 'long int'
             info.response_code = gk::anydup(rescode, sizeof(long));
                                                              ^

Why am I getting this and what is the solution to my problem? Any help will be greatly appreciate and thank you in advance.

您正在传递rescode ,这是一个很长的代码,其中代码需要一个vector<T>作为anydup()的第一个参数。

You should specify the type for template function, like this:

info.response_code = gk::anydup<long>(rescode, sizeof(long));

Upd: and anydup returns vector, but you assign it to long.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM