#include <iostream>
using namespace std;
class A {
public:
void function( int num);
bool function1()const;
virtual bool function2() const=0;
};
class B:public A {
public :
bool function2()const;
};
int _tmain(int argc, _TCHAR* argv[])
{
void (A::* p)(int)= &A::function; //不是地址,而是一个指向成员函数的指针
// Edit: Google translation of the above comment is
// "Not address, but a pointer to a member function pointer"
bool (A::* p1)()const =&A::function1; // 指向成员函数的指针可以指向一个常量成员函数
// Edit: Google translation of the above comment is
// "Point to a member function pointer can point to a const member function"
B b;
A *a=&b;
(a->*p1)();
(b.*p1)();
return 0;
}
但是当我链接它时:
1>c.obj : error LNK2019: unresolved external symbol "public: bool __thiscall A::function1(void)const " (?function1@A@@QBE_NXZ) referenced in function _wmain
1>c.obj : error LNK2019: unresolved external symbol "public: void __thiscall A::function(int)" (?function@A@@QAEXH@Z) referenced in function _wmain
1>c.obj : error LNK2001: unresolved external symbol "public: virtual bool __thiscall B::function2(void)const " (?function2@B@@UBE_NXZ)
你可以告诉我为什么吗?
答案 0 :(得分:2)
您尚未实施A::function()
,A::function1()
或B::function2()
。你需要这样做。
答案 1 :(得分:1)
A::function1
,A::function
和B::function2
都已声明,但从未定义过。如果未定义函数,则无法获得指向该函数的指针,它指向何处?