在c++std::函数的上下文中无效使用void表达式
invalid use of void expression in context of c++ std::function
在调用回调函数时出现以下代码段中的"无效使用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
的构造函数。C++中不允许传递void
,这就是编译器抱怨的原因。(Callback(10)
是此处的">空隙表达式"。(
我想这就是你想要的
#include <iostream>
#include <functional>
using namespace std;
template<class type>
class State {
public:
State(type type1,const std::function<void (type)> callback)
{
callback(type1);
}
};
template <class type>
void Callback(type type1 )
{
}
int main()
{
State<int> obj(10, Callback<int>);
return 0;
}
我想使用lambda表达式方法来避免混淆:
#include <iostream>
#include <functional>
using namespace std;
template<class type>
class State
{
public:
State( type type1, const std::function<void (type type1 )> Callback)
{
Callback(type1);
}
};
int main()
{
State<int > monitor(10,[] ( int fault) {std::cout<<"Any Message"; });
return 0;
}
相关文章:
- 错误:从'void*'到'const uint8_t* {aka const unsigned char*}'的转换无效 [-允许]
- 从类型"void*"到函数指针的强制转换无效
- 在c++std::函数的上下文中无效使用void表达式
- 从类型"void*"到类member_function指针的强制转换无效
- 无效地将 void 表达式与队列和 .pop() 一起使用
- 递归中无效的 void 操作数
- 错误:在 DLSYSM 的情况下,从“void*”到“void (*)()”的转换无效
- 为什么pthread_join给出此错误?[错误] 从'void*'到'void**'的转换无效 [-允许]
- 从“ void*”到“ pthread_t*”的无效转换
- 从"void*"到"void(*)(void*)"的转换无效
- 使用多线程处理对象数组 - 无效使用 void 表达式错误
- 从 void(WINAPI*)(..) 到 LPVOID 的强制转换无效
- 错误:从 'int' 到 'void*' 的转换无效 [-允许]
- 组合模板参数形成函数签名时无效的 void 参数
- mingw 编译错误:从 'FARPROC ' 到 'void*' 的转换无效,但 msvc 编译正常
- C STD ::线程无效的void表达式使用
- 从文件读取链表C++从 void* 到 char* 的转换无效
- 墨西哥:错误:从“void*”到“double*”的转换无效[-允许]
- 从类型 'MyStruct' 转换为类型 'void*' 无效
- 为什么 std::p ackaged_task<void()> 无效?