[英]cannot convert ‘void (myClass::*)() to void (*)()
So, I know this question exists in so many places. 所以,我知道这个问题存在于很多地方。 But none of the examples helped me solve my issue. 但是这些例子都没有帮助我解决我的问题。
I'm trying to create a method pointer (within a class), so it will address one of several methods of the class according to specific conditions. 我正在尝试创建一个方法指针 (在一个类中),因此它将根据特定条件解决该类的几种方法之一。
I tried, unsuccessfully to use a static function (guess I misunderstood the instructions how to do so...). 我试过,使用静态函数失败了(猜测我误解了说明怎么做......)。
here is the header file: 这是头文件:
class myClass
{
public:
myClass(int value);
void methodA(const string &msg);
void methodB(const string &msg);
void (*send_msg)(const string &msg);
};
and the cpp: 和cpp:
myClass::myClass(int value){
if(value > 0){
send_msg = &methodA
}
else{
send_msg = &methodB
}
}
and the errors, as some of you already know: 和你们中的一些人已经知道的错误:
error: ISO C++ forbids taking the address of an unqualified or parenthesized non-static member function to form a pointer to member function. 错误:ISO C ++禁止获取非限定或带括号的非静态成员函数的地址,以形成指向成员函数的指针。 Say '&myClass::methodA' [-fpermissive] 说'&myClass :: methodA'[-fpermissive]
error: cannot convert 'void (myClass:: )(const string&) {aka void (myClass:: )(const std::basic_string&)}' to 'void ( )(const string&) {aka void ( )(const std::basic_string&)}' in assignment 错误:无法将'void(myClass :: )(const string&){aka void(myClass :: )(const std :: basic_string&)}'转换为'void( )(const string&){aka void( )(const std: :basic_string&)}'在赋值中
any help will be much appreciated. 任何帮助都感激不尽。
Edit Thanks to Jose, it can be compile: 编辑感谢Jose,它可以编译:
void (myClass::*send_msg)(const string &msg);
and the assignment: 和任务:
send_msg = &myClass::methodA;
But now, as I'm trying to use the code I cannot call the function-pointer: 但是现在,因为我正在尝试使用代码,所以无法调用函数指针:
this->myClass_instance.send_msg(line); //*****getting error*****
when this
is an instance of other class. 当this
是其他类的实例时。 The error is: 错误是:
error: must use '. 错误:必须使用'。 ' or '-> ' to call pointer-to-member function in '((otherClass*)this)->otherClass::myClass_instance.myClass::send_msg (...)', eg '(... ->* ((otherClass*)this)->otherClass::myClass_instance.myClass::send_msg) (...)' '或' - > '在'((otherClass *)this中调用指向成员函数的函数 - > otherClass :: myClass_instance.myClass :: send_msg(...)',例如'(... - > * ((otherClass *)this) - > otherClass :: myClass_instance.myClass :: send_msg)(...)'
My demo: http://ideone.com/Vx4x88 我的演示: http : //ideone.com/Vx4x88
Make send_msg a pointer to a class method by adding myClass::
. 通过添加myClass::
使send_msg成为指向类方法的指针。
void (myClass::*send_msg)(const string &msg);
Also, it seems like you are missing myClass::
and a semicolon ;
此外,您似乎缺少myClass::
和分号;
at the end of 在......的最后
send_msg = &myClass::methodA;
else
send_msg = &myClass::methodB;
Edit: 编辑:
Since your requested how to call it from outside the class in your comment: 由于您的请求如何在评论中从课外调用它:
myClass c(1);
(c.*c.send_msg)("hi");
or 要么
myClass * p = new myClass(1);
(p->*p->send_msg)("hi");
void (*send_msg)(const string &msg);
is declaration of pointer for free function or static member function, not non-static member function. 是自由函数或静态成员函数的指针声明,而不是非静态成员函数。 You might want: 你可能想要:
void (myClass::*send_msg)(const string &msg);
Or you could make the functions to be static member function: 或者你可以使函数成为静态成员函数:
static void methodA(const string &msg);
static void methodB(const string &msg);
For the 1st compile error, you should qualify name of non-static member function when taking its address (for the 1st solution only): 对于第一个编译错误,您应该在获取其地址时限定非静态成员函数的名称(仅针对第一个解决方案):
myClass::myClass(int value){
if(value > 0){
send_msg = &myClass::methodA;
~~~~~~~~~
}
else{
send_msg = &myClass::methodB;
~~~~~~~~~
}
}
you can also use std::function
你也可以使用std::function
#include <functional>
class myClass
{
public:
myClass(int value){
if(value > 0){
send_msg = [this](const std::string& str){ methodA(str); };
}
else{
send_msg = [this](const std::string& str){ methodB(str); };
}
}
void methodA(const string &msg);
void methodB(const string &msg);
std::function<void(const string &msg)> send_msg;
};
to call the function, simply use ()
: 要调用该函数,只需使用()
:
myClass obj(10);
obj.send_msg("hi there");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.