函数指针调用不编译 [英] Function pointer call doesn't compile
本文介绍了函数指针调用不编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我只是没有得到它,为什么行22未能编译?
#include< stdexcept>
#include< dlfcn.h>
#includeLibrary.h
int main(int argc,char * argv [])
{
try
{
void * libHandle = 0;
libHandle = dlopen(libExpandableTestLibrary.so,RTLD_LAZY);
if(!libHandle)
throw std :: logic_error(dlerror());
std :: cout<< Libary优雅地打开< std :: endl;
void * fuPtr = 0;
fuPtr = dlsym(libHandle,createLibrary);
if(!fuPtr)
throw std :: logic_error(dlerror());
库* libInstance = static_cast< Library *()>(fuPtr)();
//教程:http://www.linuxjournal.com/article/3687
//教程代码:shape * my_shape = static_cast< shape *()>(mkr)();
//编译器错误消息:Application.cpp:22:56:error:invalid static_cast从类型'void *'到类型'Library *()'
libInstance-> Foo
dlclose(libHandle);
} catch(std :: exception& ex)
{
std :: cerr< ex.what()<< std :: endl;
}
}
欢迎任何帮助
如果您需要
我认为 fuPtr
指向一个函数,该函数应该返回一个指向 Library
对象的指针(假定加载的名称为createLibrary
)。
在这种情况下,包含您的演员的行需要如下:
Library * libInstance = reinterpret_cast< Library *(*)()>(fuPtr)();
I just dont get it, why line 22 is failing to compile?
#include <stdexcept>
#include <dlfcn.h>
#include "Library.h"
int main(int argc, char *argv[])
{
try
{
void* libHandle = 0;
libHandle = dlopen("libExpandableTestLibrary.so", RTLD_LAZY);
if(!libHandle)
throw std::logic_error(dlerror());
std::cout << "Libary opened gracefully" << std::endl;
void* fuPtr = 0;
fuPtr = dlsym(libHandle, "createLibrary");
if(!fuPtr)
throw std::logic_error(dlerror());
Library* libInstance = static_cast<Library* ()>(fuPtr)();
// Tutorial: http://www.linuxjournal.com/article/3687
// Tutorial Code: shape *my_shape = static_cast<shape *()>(mkr)();
// Compiler error message: Application.cpp:22:56: error: invalid static_cast from type ‘void*’ to type ‘Library*()’
libInstance->Foo();
dlclose(libHandle);
} catch(std::exception& ex)
{
std::cerr << ex.what() << std::endl;
}
}
Any help is welcome If you need additional information's just let me know.
解决方案
I take it that fuPtr
points to a function that is supposed to return a pointer to a Library
object (given the name loaded is "createLibrary"
).
In that case, the line including your cast needs to look like this:
Library* libInstance = reinterpret_cast<Library* (*)()>(fuPtr)();
这篇关于函数指针调用不编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文