无法将参数 1 从"int (__thiscall A::* *)(void)"转换为"int (__cdecl *)(void)"
Cannot convert parameter 1 from 'int (__thiscall A::* *)(void)' to 'int (__cdecl *)(void)'
我在运行此代码时收到此错误。请查看我的代码并协助我。
#include "stdafx.h"
#include <iostream>
class A
{
public:
void PrintTwoNumbers(int (*numberSource)(void))
{
int val1= numberSource();
}
int overNineThousand(void)
{
return (rand()%1000) + 9001;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
int (A::*fptr) (void) = &A::overNineThousand;
int (A::*fptr1) (void);
fptr1 = &A::overNineThousand;
A a;
a.PrintTwoNumbers(&fptr); //-> how to pass here
getchar();
return 0;
}
我厌倦了在网上搜索这个,没有人为此提供完美的解决方案。任何人都可以将此代码编辑为工作代码并帮助我吗?
预期的参数是一个(非成员(函数指针。而是传递指向成员函数的(指向 a( 指针。(指向(指向成员函数的指针不能转换为指向(非成员(函数的指针。
可能最简单的解决方案是将函数参数固定为正确的类型,传递隐式对象参数,并且在调用时不要获取成员函数指针的地址。
void PrintTwoNumbers(int (A::*numberSource) ())
{
int val1= (this->*numberSource)();
}
a.PrintTwoNumbers(fptr);
相关文章:
- 为什么野牛仍在使用"int yylex(void)",却找不到"int yylex(YYS
- <Windows>为什么 std::thread::native_handle 返回类型为"long long unsigned int"的值,而不是 void*(又名 HANDLE)?
- 为什么 c++ 中的 main() 函数不采用除 int 和 void 之外的任何其他返回类型
- 不能使这种类型的"void(C::* volatile)(int) const "在参考手册C++示
- 如何使模板函数像 R f<void(int)>(args...)
- template<class T, int N> void h(T const(&)[N]); as friend function
- 函数 LNK2019 "int __cdecl __scrt_common_main_seh(void)" 中引用的未解析外部符号主错误 (?__scrt_common_main_seh@@YAHXZ
- 从不兼容的类型 'void (Button::*)(int)' 分配给'void (*)(int)'
- 错误:无法将"打印"从"void (*)(int)"转换为"std::function<void()>
- 标准的大小::函数<void(int&)>类型
- std::bind1st是否可以用于将void(*)(void*,int)转换为void(*int)
- "void (*)(int)"与"void (^)(int)"相同吗?
- boost::function<void (int)> 类型兼容性,是怎么做到的?
- void(int) 和 void (*)(int) 之间的区别
- 在Linux上编译简单信号代码时,从void*到void (*) int的无效转换错误
- 错误:将'const Tarih'作为'void(int)' 'this'参数传递丢弃限定符|
- void(int) 和 void(*)(int) 有什么区别?
- 警告(不合时宜):将 void(*)(int) 分配给 extern "C" void(*)(int)
- 为什么 C++11 不支持 'std::function<void(int, ...)>'?
- 为什么在void/int声明后使用const