2010-08-31 88 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

相關問題