【发布时间】:2013-03-03 12:08:19
【问题描述】:
我想从 lib 中的一个类中使用指针访问我的主类的成员函数。
我可以访问主类中的公共变量。但是当我尝试访问成员函数(公共)时,我有这个错误:
./Panda: symbol lookup error: libkoala.so: undefined symbol: _ZNK5Panda6getLOLEv
我真的不知道为什么,也许我没有理解 c++ 中的一个概念......
我的主要课程:
class Panda
{
protected:
IAssistant* (*external_creator)();
IAssistant* lib;
void* dlhandle;
int lol;
public:
int hello;
Panda();
~Panda();
int getLOL() const;
};
Panda::Panda()
{
if ((dlhandle = dlopen("libkoala.so", RTLD_LAZY)) == NULL)
exit(-1);
if ((external_creator = reinterpret_cast<IAssistant* (*)()>(dlsym(dlhandle, "create_assistant"))) == NULL)
exit(-1);
lib = external_creator();
lol = 69;
hello = 42;
lib->do_something(this);
}
Panda::~Panda(){
}
int Panda::getLOL() const
{
return lol;
}
界面是这样的:
class IAssistant
{
public:
virtual void do_something(Panda *) = 0;
};
还有我的库中的类:
class Koala : public IAssistant
{
public:
void do_something(Panda *);
};
void Koala::do_something(Panda * ptr)
{
std::cout << ptr->hello; <========= work perfectly
std::cout << ptr->getLOL(); <====== doesn't work
}
extern "C"
{
IAssistant* create_assistant()
{
return new Koala();
}
}
你有什么想法吗?
【问题讨论】:
-
@Luchian 就在第一个代码 sn-p 的底部。
-
好像你有循环依赖...
-
为什么你仍然把
virtual关键字放在Koala::do_something上,因为你是从IAssistant覆盖它的?
标签: c++ pointers dll member-function-pointers