2011-12-13 121 views
2

我正在使用包含DLL的可執行文件。對於我的測試用例,我將代碼組合成一個可執行文件。我正在使用Visual Studio 2008和Boost 1.43。我試過研究這個,但還沒有找到明確的答案。謝謝您的幫助。是否可以指向類成員的Typedef函數指針?

在我main.h:

#include <string> 

//These are normally defined in a seperate DLL 
typedef std::string Typedef_func(const std::string & title); 
void Register_My_Typedef(Typedef_func*); 
//------------------------------------------- 

class myClass 
{ 
public: 
    std::string func_one(const std::string & title); 

    Typedef_func _test; 

    void run(); 
}; 

在我的main.cpp:

#include "main.h" 
#include <boost/bind.hpp> 

std::string workingFunc(const std::string & title) 
{ 
    return ""; 
} 

int main(int argc, char* argv[]) 
{ 
    myclass* example; 
    example->run(); 

    Register_My_Typedef(&workingFunc);//This works. 

    return 0; 
} 

void myClass::run() 
{ 
    //I want to point a Typedef_func* in a DLL to call myclass::func_one 
    Typedef_func* tf = boost::bind(&myClass::func_one, this, "test"); //This does not. 

    Register_My_Typedef(tf); 
} 

std::string myClass::funcOne(const std::string & title) 
{ 
    return ""; 
} 

void Register_My_Typedef(Typedef_func* passedIn) 
{ 
    //Points the pointer in the DLL to passedIn 
} 

的DLL邏輯正常工作時Register_My_Typedef叫不上一個類中的功能,但它是可能從一個類中調用它?當我嘗試編譯這段代碼返回的是:當我嘗試

,並在Windows XP中使用VS2008編譯我得到:

錯誤C2440:初始化:不能從 轉換「的boost :: _ BI: :bind_t'到'Typedef_func(__cdecl *)' [ R = std :: string, F = boost :: _ mfi :: mf1, L = boost :: _ bi :: list2,boost :: _ bi ::值> ]

沒有可執行此操作的用戶定義轉換操作員 轉換,否則操作員不能被調用。

回答

-1

答案是本身的typedef是類成員的靜態成員函數&行爲不同非靜態但效果最好讀,而用戶定義的類主要的類函數。