#include<iostream>
#include<functional>
#include <unordered_set>
#include <vector>
#include <unordered_map>
#include <variant>
class Button
{
public:
Button(int _val):val(_val){}
~Button();
int val;
template<typename F, typename...InArgs>
void addListener(std::function<F(InArgs...)>(f), InArgs... args) {
listener.push_back(f(args...));
}
template<typename F, typename...InArgs>
void callMethod(std::function<F(InArgs...)>(f), InArgs... args) {
f(args...);
}
void print() { std::cout << "Button value -> " << val << std::endl; }
private:
template<typename F,typename...InArgs>
static std::vector<std::function<F(InArgs...)>> listener;
};
inline void test1() {
std::cout << "Printing test1 method...\n";
}
inline void test2(Button* b) {
std::cout << "Button value is 20.\n";
b->val = 20;
}
inline void test3(Button* b, int a) {
std::cout << "Button value is passed parameter.\n";
b->val = a;
}
inline void test4(int a) {
std::cout << "print " << a << std::endl;
}
class AppThree
{
public:
static void run() {
Button* button = new Button(5);
button->print();
//button->callMethod(std::function<void(int)>(&test4), 1); //work it
button->addListener(std::function<void(int)>(&test4),1); // error
}
};
我想将带参数的函数添加到向量中。 我想从向量中调用所有的函数,但是我无法添加函数。 我遇到了C3245错误。 我不知道哪里出错了。 如果我的想法不正确,请指出正确的方法。
“function”:使用变量模板需要模板参数列表
- JMCamposlistener
,这意味着你有一个向量对应于每个函数类型。编译器无法推断你指的是哪一个。你还试图push_back
调用函数的结果,而不是函数本身。 - molbdnilo