无法使用 wistringstream 实例化 istring_iterator

发布于 2024-09-24 04:30:27 字数 1332 浏览 1 评论 0原文

我正在尝试使用此线程,但我正在尝试将其适应 wstring。然而,我偶然发现了一个奇怪的错误。检查代码:

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

确切的错误消息是:

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

我认为它是说它无法使用传递的 iss 实例化 istream_iterator (这是一个 wistringstream 而不是字符串流)。这是在使用 XCode 和 GCC 4.2 的 Mac 上进行的。而且据我所知,没有 wistring_iterator 或类似的东西。

当我使用非 wstring 版本时它工作得很好。正如您可能看到的,我已更改声明以使用 wstringwistringstreamvector,只是替换了所有内容以使用宽版本。

什么可能导致此问题以及为什么它不接受 wstring 版本?

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 wistringstream 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甲如呢乙后呢 2024-10-01 04:30:27

根据错误消息的后半部分,您必须覆盖 istream_iterator() 上的默认参数 2 和 3 以匹配其他地方的 Widechar 使用。在 Visual C++ 中,这个版本对我来说编译正常:

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

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));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文