在下面的代码片段中,调用回调函数时出现“无效使用 void 表达式”错误 由编译器刷新。
#include <iostream>
#include <functional>
using namespace std;
template<class type>
class State {
public:
State(type type1,const std::function<void (type type1 )> Callback)
{
}
};
template <class type>
void Callback(type type1 )
{
//Based on type validation will be done here
}
int main()
{
State<int> obj(10,Callback(10));
return 0;
}
只是想知道这里出了什么问题,以便解决同样的问题。
最佳答案
看来你想通过Callback<int>
函数本身,而不是它的返回值(没有),到 obj
的构造函数。所以就这样做:
State<int> obj(10, Callback<int>);
您当前的代码实际上调用 Callback(10)
首先,然后尝试获取其 void
“返回值”将其传递给 obj
的构造函数。路过void
在 C++ 中是不允许的,这就是编译器提示的原因。 (Callback(10)
在这里是“无效表达式”。)
关于c++ - 在 c++ std::function 上下文中无效使用 void 表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58646851/