![](/img/trans.png)
[英]How do I define a template member function outside of a full specialized template class's definition?
[英]How do I redefine a class's function outside of the class
class Function
{
public:
std::string Name;
void call(std::string x);
Function(std::string Nam)
{
Name = Nam;
}
};
std::vector<Function> funcs;
void Load_FuncLib()
{
Function print("print");
Function add("add");
print.call(std::string x)
{
std::cout<< x <<"\n";
}
add.call(std::string x)
{
std::cout<< std::stoi(x) + std::stoi(x) << "\n";
}
funcs.push_back(print);
funcs.push_back(add);
funcs.at(0).call("Hello world");
}
我希望它运行功能print.call("Hello world");
但是它不起作用,因为我不知道如何设置已经声明的函数,也不知道如何使用向量调用它。
您最有可能想要实现这样的目标?
#include <unordered_map>
#include <iostream>
#include <string>
#include <functional>
int main() {
std::unordered_map<std::string, std::function<void (const std::string&)>> funcs;
funcs["print"] = [](const std::string& str) {
std::cout << str << '\n';
};
funcs["add"] = [](const std::string& str) {
int i = std::stoi(str);
std::cout << i + i << '\n';
};
funcs["print"]("Hello, World!");
funcs["add"]("12");
}
您随时可以使用其他函数重置哈希图的某个值。 另外,根据您的需要,您可以使用std :: function或仅使用普通函数指针。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.