2012-04-01 120 views
1

我有以下代碼,其中Boost.Local使用函數回調來加載mo文件。該函數對我來說叫做findMo,我試圖將它綁定到一個對象上,這樣我就可以保留我放在moFinder的私有成員中的副作用。Boost.Bind非靜態成員

class moFinder 
{ 
    public: 
    moFinder(string const& wantedFormat) 
    : format(wantedFormat) 
    { 
     // ... 
    } 

    std::vector<char> findMo(std::string const& filePath, std::string const& encoding) 
    { 
     // ... 
    } 
}; 

std::locale createLocale(string const& name, string const& customTranslation, 
    string const& path, string const& domain, string const& pathFormat) 
{ 
    // ... 

    namespace blg = boost::locale::gnu_gettext; 
    blg::messages_info info; 
    info.paths.push_back(path); 
    info.domains.push_back(blg::messages_info::domain(domain)); 

    moFinder finder(pathFormat); 

    blg::messages_info::callback_type callbackFunc; 
    callbackFunc = boost::bind(moFinder::findMo, boost::ref(finder)); 

    info.callback = callbackFunc; 

    // ... 
} 

編譯時,我得到以下錯誤:

error: invalid use of non-static member function ‘std::vector moFinder::findMo(const std::string&, const std::string&)’

在那裏我打電話的boost ::綁定就行了。

我在做什麼值得這個錯誤?

+0

儘量減少您的問題,以較小的樣本。這裏沒有什麼與'boost.locale'有關的。它純粹是「綁定」。這使得診斷更容易。 – pmr 2012-04-01 13:06:31

+0

我通常會這樣做,但我真的不確定它是否與Boost.Locale怪異有關。 – Jookia 2012-04-01 13:19:22

回答

5

您在成員地址前缺少運營商地址:&moFinder::findMo。此外,您需要使用boost::mem_fn將成員函數包裝到函數對象中,並且缺少佔位符。總而言之:

boost::bind(boost::mem_fn(&moFinder::findMo,), boost::ref(finder), _1, _2); 
               // or &finder 
+0

謝謝!我從來沒有見過boost :: mem_fn或_1,_2s在查看其他示例的stackoverflow之前,我錯過了什麼? – Jookia 2012-04-01 13:21:06

+1

@Jookia boost.bind文檔? http://www.boost.org/doc/libs/1_49_0/libs/bind/bind.html;) – pmr 2012-04-01 13:24:04