2017-10-15 100 views
1

我試圖將一個字符串映射到函數。該功能應該得到一個const char*中過去了。我很奇怪,爲什麼我不斷收到這將函數映射到字符串

*no match for call to ‘(boost::_bi::bind_t<boost::_bi::unspecified, void (*)(const char*), boost::_bi::list0>) (const char*)’* 

我的代碼如下

#include <map> 
#include <string> 
#include <iostream> 
#include <boost/bind.hpp> 
#include <boost/function.hpp> 



typedef boost::function<void(const char*)> fun_t; 
typedef std::map<std::string, fun_t> funs_t; 



void $A(const char *msg) 
{ 
    std::cout<<"hello $A"; 
} 

int main(int argc, char **argv) 
{ 
    std::string p = "hello"; 
    funs_t f; 
    f["$A"] = boost::bind($A); 
    f["$A"](p.c_str()); 
    return 0; 
} 
+0

我會提醒你不要使用非標準的標識符,比如'$ A'。 – StoryTeller

回答

1

在你的榜樣錯誤,使用boost::bind完全是多餘的。你可以直接指定函數本身(它將被轉換爲一個指向函數的指針,並且被boost::function刪除)。

既然你確實綁定了,僅僅傳遞函數是不夠的。綁定時需要給出boost::bind參數,或者指定佔位符如果您希望綁定對象將某些內容轉發給您的函數。你可以在錯誤信息中看到它,這就是boost::_bi::list0

因此,要解決這個問題:

f["$A"] = boost::bind($A, _1); 

或者簡單的

f["$A"] = $A; 

而且,正如我在註釋中提到你,我建議你避開那些不規範的標識符。 A $在根據C++標準的標識符中不是有效的標記。一些實現可能支持它,但並非所有的都需要。

相關問題