问题描述
在下面的代码段中,调用回调函数无效使用void表达式"时出错由编译器刷新.
In below code snippet while calling call back function "Invalid use of void expression" erroris flashed by the compiler.
#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;
}
只想知道这里有什么问题,以便可以解决.
Just want to know what is the wrong here so that same can be addressed .
推荐答案
似乎您想传递 Callback< int>
函数本身,而不是其返回值(没有返回值),到 obj
的构造函数.就是这样:
It seems that you want to pass the Callback<int>
function itself, not its return value (which there is none), to the constructor of obj
. So do just that:
State<int> obj(10, Callback<int>);
您当前的代码实际上先调用 Callback(10)
,然后尝试获取其 void
返回值"以将其传递给 obj
的构造函数.在C ++中不允许传递 void
,这就是编译器抱怨的原因.( Callback(10)
是此处的" void expresson ".)
Your current code actually calls Callback(10)
first and then tries to take its void
"return value" to pass it to the constructor of obj
. Passing void
is not allowed in C++, which is why the compiler is complaining. (Callback(10)
is the "void expresson" here.)
这篇关于在C ++ std :: function上下文中无效使用void表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!