我有一大段代码有这个问题,其中有一些事件处理对象。在一个事件处理类中,我试图通过指针调用另一个类的函数,但它与这个代码有相同的错误,我已经实现了这个代码来检查我用来在那里调用的逻辑。我在这里做错了什么?
#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();
}
取消注释函数调用后的错误
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 ==========