【问题标题】:Why does istream_iterator<unsigned char, unsigned char> throw std::bad_cast?为什么 istream_iterator<unsigned char, unsigned char> 会抛出 std::bad_cast?
【发布时间】:2011-04-06 07:36:48
【问题描述】:

发生了什么事?

#include <iostream>
#include <iterator>
#include <sstream>

int main() {
    std::basic_stringbuf<unsigned char> buf;
    std::basic_istream<unsigned char> stream(&buf);
    // the next line throws std::bad_cast on g++ 4.4
    std::istream_iterator<unsigned char, unsigned char> it(stream);
}

我在构造迭代器之前尝试过stream.write(some_array, sizeof(some_array),但无济于事。

谢谢。

【问题讨论】:

  • 在 VS8 上顺利通过,但我不相信!!

标签: c++ stl iostream istream istream-iterator


【解决方案1】:

它从哨兵对象的构造函数中抛出,在那里它检查流上的 ctype facet(它需要它以便它可以跳过空格),它恰好是 NULL,因为它没有为无符号字符定义。

您需要处理该流上的空格吗?如果没有,请更改为

std::istreambuf_iterator<unsigned char> it(stream);

【讨论】:

  • 是的,就是这样。或者,我猜stream.unsetf(std::ios::skipws) 具有相同的效果。谢谢。
【解决方案2】:

应该是:

std::istream_iterator<unsigned char> it(stream);

【讨论】:

  • 那不编译。 ("没有匹配的函数调用 std::istream_iterator...")
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-07
  • 1970-01-01
  • 2013-11-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多