2010-08-31 98 views
2

发生了什么事?为什么istream_iterator <unsigned char,unsigned char> throw std :: bad_cast?

#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),无济于事。

谢谢。

+0

通过VS8没有任何打嗝,但我不会相信它! – DumbCoder 2010-08-31 14:39:30

+0

也在VS10上(刚测试过)。 – 2010-08-31 14:44:35

回答

2

它从岗哨对象的构造,它检查在流(它需要它,以便它可以跳过空白),这恰好是NULL因为它没有限定的ctype方面抛出为无符号的字符。

你需要处理那个流上的空白吗?如果没有,更改为

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

是的,这是它。或者,我猜'stream.unsetf(std :: ios :: skipws)'具有相同的效果。谢谢。 – 2010-08-31 14:54:04

0

不应该它是:

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

这不会编译。 (“没有匹配函数调用std :: istream_iterator ...”) – 2010-08-31 14:45:58

相关问题