尝试将函数作为参数传递时未解析的重载函数类型 p>我在尝试用g++ + C++ 11编写一个孤立的C++测试文件时,收到以下错误。 spike/cur_spike.cpp: In function ‘int main()’: spike/cur_spike.cpp:60:44: error: no matching function for call to ‘callFunctionFromName(<unresolved overloaded function type>, std::__cxx11::string&)’ callFunctionFromName (outputLine, param); ^ spike/cur_spike.cpp:49:7: note: candidate: template<class funcT, class ... Args> funcT callFunctionFromName(funcT (*)(Args ...), Args ...) funcT callFunctionFromName (funcT func(Args...), Args... args) { ^ spike/cur_spike.cpp:49:7: note: template argument deduction/substitution failed: spike/cur_spike.cpp:60:44: note: couldn't deduce template parameter ‘funcT’ callFunctionFromName (outputLine, param); ^ spike/cur_spike.cpp:在函数“int main()”中: spike/cur_spike.cpp:60:44:错误:调用“callFunctionFromName(,std::_cxx11::string&)”时没有匹配的函数 callFunctionFromName(输出线,参数); ^ spike/cur_spike.cpp:49:7:注:候选:模板函数调用函数fromname(funcT(*)(Args…,Args…) funcT callFunctionFromName(funcT func(Args…,Args…Args){ ^ spike/cur_spike.cpp:49:7:注意:模板参数推断/替换失败: spike/cur_spike.cpp:60:44:注意:无法推断模板参数“funcT” callFunctionFromName(输出线,参数); ^
这是密码尝试将函数作为参数传递时未解析的重载函数类型 p>我在尝试用g++ + C++ 11编写一个孤立的C++测试文件时,收到以下错误。 spike/cur_spike.cpp: In function ‘int main()’: spike/cur_spike.cpp:60:44: error: no matching function for call to ‘callFunctionFromName(<unresolved overloaded function type>, std::__cxx11::string&)’ callFunctionFromName (outputLine, param); ^ spike/cur_spike.cpp:49:7: note: candidate: template<class funcT, class ... Args> funcT callFunctionFromName(funcT (*)(Args ...), Args ...) funcT callFunctionFromName (funcT func(Args...), Args... args) { ^ spike/cur_spike.cpp:49:7: note: template argument deduction/substitution failed: spike/cur_spike.cpp:60:44: note: couldn't deduce template parameter ‘funcT’ callFunctionFromName (outputLine, param); ^ spike/cur_spike.cpp:在函数“int main()”中: spike/cur_spike.cpp:60:44:错误:调用“callFunctionFromName(,std::_cxx11::string&)”时没有匹配的函数 callFunctionFromName(输出线,参数); ^ spike/cur_spike.cpp:49:7:注:候选:模板函数调用函数fromname(funcT(*)(Args…,Args…) funcT callFunctionFromName(funcT func(Args…,Args…Args){ ^ spike/cur_spike.cpp:49:7:注意:模板参数推断/替换失败: spike/cur_spike.cpp:60:44:注意:无法推断模板参数“funcT” callFunctionFromName(输出线,参数); ^,c++,function,templates,c++11,variadic-templates,C++,Function,Templates,C++11,Variadic Templates,这是密码 #include <iostream> #include <string> template <typename T> void outputLine (T text) { std::cout << text << std::endl; } template <typename funcT> funcT callFunctionFromName (funcT func()) { return
#include <iostream>
#include <string>
template <typename T>
void outputLine (T text) {
std::cout << text << std::endl;
}
template <typename funcT>
funcT callFunctionFromName (funcT func()) {
return func ();
}
template <typename funcT, typename... Args>
funcT callFunctionFromName (funcT func(Args...), Args... args) {
return func (args...);
}
int main () {
std::string param = "Testing...";
callFunctionFromName (outputLine, param);
return 0;
}
#包括
#包括
模板
无效输出行(T文本){
std::cout您可以手动设置模板参数
callFunctionFromName (outputLine<std::string>, param);
callFunctionFromName(outputLine,param);
好的,呃,那是有效的。我不知道为什么。你能给我一个链接或一些参考资料,我可以阅读关于这个代码< >代码>语法吗?我是C++的新手,很抱歉这个看似无知的问题。谢谢!!WiTro,它只是函数的显式实例化。你告诉编译器,你想使用<代码> OuttoPox< /Cult>类型<代码>td::string
。啊哈,好吧,谢谢。但是没有办法让它保持通用性,也就是说,如果我想通过callFunctionFromName
将一个值传递给outputLine
,直到传递的那一刻我才知道它的类型吗?@Wintro编译器不知道什么函数是outputLine,它只是
。为什么无法在调用站点中手动指定函数类型?哦,因此编译器无法确定函数的确切类型,除非我指定所需的重载。我现在就知道了。谢谢。我可以指定类型,但我只是作为一般问题提问。