views:

28

answers:

1

I'm trying to split a string using the method found in this thread, but I'm trying to adapt it to a wstring. However, I have stumbled upon a weird error. Check the code:

#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <algorithm>
#include <iterator>

using namespace std;

int main(void)
{
    wstring str(L"Börk börk");
    wistringstream iss(str);
    vector<wstring> tokens;
    copy(istream_iterator<wstring>(iss), // I GET THE ERROR HERE
         istream_iterator<wstring>(),
         back_inserter< vector<wstring> >(tokens));

    return 0;
}

The exact error message is:

error: no matching function for call to 'std::istream_iterator<std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >, char, std::char_traits<char>, int>::istream_iterator(std::wistringstream&)'

I think it is saying that it can't instantiate the istream_iterator using the passed iss (which is a *w*istringstream instead of a istringstream). This is on a Mac using XCode and GCC 4.2. And AFAIK there is no wistring_iterator or anything like that.

It works perfectly when I'm using the non-wstring version. As you might see I have changed the declarations to use wstring, wistringstreams and vector<wstring>, just replaced everything to use the wide version.

What could cause this and why won't it accept the wstring-version?

+1  A: 

Per the latter part of the error message, you have to override the default params 2 and 3 on istream_iterator() to match widechar usage elsewhere. In Visual C++ this version compiles OK for me:

copy(istream_iterator<wstring, wchar_t, std::char_traits<wchar_t> >(iss), // I DO NOT GET THE ERROR HERE
    istream_iterator<wstring, wchar_t, std::char_traits<wchar_t> >(),
    back_inserter< vector<wstring> >(tokens));
Steve Townsend