2014-09-27 24 views
0

我有一個提升字符串算法庫的問題。 我試圖分裂和標記化拆分/記號化wstring的,但我總是得到這個以下錯誤提升字符串算法錯誤

error C2664: 'std::_String_const_iterator<std::_String_val<std::_Simple_types<char>>>::_String_const_iterator 
(const std::_String_const_iterator<std::_String_val<std::_Simple_types<char>>> &)' : 
cannot convert argument 1 
from 'std::_String_const_iterator<std::_String_val<std::_Simple_types<wchar_t>>>' 
to 'const std::_String_const_iterator<std::_String_val<std::_Simple_types<char>>> &' 

我已經嘗試過其他的代碼

std::vector<std::wstring> tokenize(const std::wstring& input) { 
    std::vector<std::wstring> output; 
    boost::char_separator<wchar_t> sep(L";"); 
    boost::tokenizer<boost::char_separator<wchar_t>> tokens(input, sep); 
    std::for_each(tokens.begin(), tokens.end(), 
     [&output] (std::wstring ws) { 
      output.push_back(ws); 
     } 
    ); 
    return output; 
} 

錯誤消息意味着像boost::split或將wstring更改爲字符串,但它不起作用。

這裏有什麼問題?

+0

重複http://stackoverflow.com/questions/1307883/error-c2664-converting-from-from-const-stdstring-to -stdstring – 2014-09-27 15:14:08

回答

2

綜觀tokenizer.hpp源,分詞的定義是這樣的:

template < 
    typename TokenizerFunc = char_delimiters_separator<char>, 
    typename Iterator = std::string::const_iterator, 
    typename Type = std::string 
    > 
    class tokenizer { 
    ... 

您只指定TokenizerFunc爲類模板,但忘了指定IteratorType。其結果是,你得到了錯誤:cannot convert argument 1 ...wchar_t... to ...char...

爲了使你的代碼運行,則應指定的boost ::標記生成器的所有參數,例如:

typedef boost::tokenizer<boost::char_separator<wchar_t>, std::wstring::const_iterator, std::wstring > tokenizer; 

代碼:

#include <stdlib.h> 
#include <iostream> 
#include <vector> 
#include <string> 
#include <algorithm> 
#include <boost/tokenizer.hpp> 

std::vector<std::wstring> tokenize(const std::wstring& input) { 
    std::vector<std::wstring> output; 

    typedef boost::tokenizer<boost::char_separator<wchar_t>, std::wstring::const_iterator, std::wstring > tokenizer; 
    boost::char_separator<wchar_t> sep(L";"); 
    tokenizer tokens(input, sep); 

    std::for_each(tokens.begin(), tokens.end(), 
    [&output] (std::wstring ws) { 
     output.push_back(ws); 
    } 
); 

    return output; 
} 

int main(int argc, char* argv[]) { 

    auto v = tokenize(L"one;two;three"); 
    std::copy(v.begin(), v.end(), std::ostream_iterator<std::wstring, wchar_t>(std::wcout, L" ")); 

    return EXIT_SUCCESS; 
} 

輸出:

one two three 
+0

我同意此處的分析。 – sehe 2014-09-28 20:31:33