我在这里提出了一个问题,得到了社区的好评:A templated 'strdup()'?
但现在,当我执行以下操作时:
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));
使用以下模板:
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;
}
我得到以下内容:
/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));
^
为什么我得到这个以及我的问题的解决方案是什么?任何帮助都将非常感谢,并提前感谢您。
答案 0 :(得分:1)
您正在试用rescode
,这是一个很长的代码需要vector<T>
的地方,作为anydup()
的第一个参数。
答案 1 :(得分:0)
您应该指定模板功能的类型,如下所示:
info.response_code = gk::anydup<long>(rescode, sizeof(long));
Upd:和anydup返回vector,但是你将它分配给long。