Evan Sebastian
Evan Sebastian

Reputation: 1744

Boost string algorithm error

I have a problem with boost string algorithm library. I tried split and tokenize to split/tokenize wstring, but I always get this following error

The code

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;
}

The error messages

 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>>> &'

I have tried other means, like boost::split or changing wstring to string, but it doesn't work.

What is wrong here?

Upvotes: 0

Views: 419

Answers (1)

Grigorii Chudnov
Grigorii Chudnov

Reputation: 3112

Looking at the source of tokenizer.hpp, tokenizer is defined as this:

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

You specified only TokenizerFunc for the class template, but forgot to specify Iterator and Type. As a result, you got that error: cannot convert argument 1 ...wchar_t... to ...char...

To make you code running, you should specify all parameters for boost::tokenizer, e.g:

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

Code:

#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;
}

Output:

one two three 

Upvotes: 2

Related Questions