I have this problem with a big piece of code which have some event handling objects. In an event handling class I am trying to call a function of another class through pointer but it is having same error as this code have which I have implemented to just check the logic that I am using to call there. What is wrong I am doing here?
#include <iostream>
using namespace std;
class FunctionPointer;
class UseFP
{
public:
UseFP(void){}
~UseFP(void){}
void Modified(int m,int n);
void (FunctionPointer::*update)(int,int);
};
void UseFP::Modified(int m,int n)
{
//(this->*update)(m,n);// call by fp if I uncomment it it gives error.
}
class FunctionPointer
{
int a,b;
UseFP * obj;
public:
FunctionPointer(void);
~FunctionPointer(void);
void updateData(int m, int n)
{
a = m;
b = n;
cout<<"\n\nUpdated: a "<<a<<", b "<<b<<endl;
}
void Input()
{
int m, n;
cout<<"\nEnter new data: ";
cin>>m>>n;
obj->Modified(m,n);
}
};
void main()
{
FunctionPointer obj;
obj.Input();
}
errors after uncommenting function call
1>------ Build started: Project: FunctionPointer, Configuration: Debug Win32 ------
1>Compiling...
1>main.cpp
1>c:\users\volmo\desktop\functionpointer\functionpointer\functionpointer.h(14) : error C2440: 'newline' : cannot convert from 'UseFP *const ' to 'FunctionPointer *const '
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>c:\users\volmo\desktop\functionpointer\functionpointer\functionpointer.h(14) : error C2647: '->*' : cannot dereference a 'void (__thiscall FunctionPointer::* )(int,int)' on a 'UseFP *const '
1>Generating Code...
1>Compiling...
1>FunctionPointer.cpp
1>c:\users\volmo\desktop\functionpointer\functionpointer\functionpointer.h(14) : error C2440: 'newline' : cannot convert from 'UseFP *const ' to 'FunctionPointer *const '
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>c:\users\volmo\desktop\functionpointer\functionpointer\functionpointer.h(14) : error C2647: '->*' : cannot dereference a 'void (__thiscall FunctionPointer::* )(int,int)' on a 'UseFP *const '
1>Generating Code...
1>Build log was saved at "file://c:\Users\volmo\Desktop\FunctionPointer\FunctionPointer\Debug\BuildLog.htm"
1>FunctionPointer - 4 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
update
is typed as a "pointer to member function of class FunctionPointer
. This means it requires a FunctionPointer
instance on the left of ->*
. But you're trying to dereference it with this
which is of type UseFP
. Hence the error.
You need an instance of FunctionPointer
to invoke update
on it. I don't know what your intended semantics is, but one way to obtain one would be to add a parameter to Modified
:
void UseFP::Modified(FunctionPointer &fp, int m,int n)
{
(fp.*update)(m,n);
}
You cannot do such things. You should pass FunctionPointer
object to function Modified
, or store as class variable.
void UseFP::Modified(FunctionPointer* p, int m,int n)
{
(p->*update)(m,n);// call by fp if I uncomment it it gives error.
}
And call it like
obj->Modified(this,m,n);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.