Stroustrup的第107页 C ++编程语言(第4版)是通过赋值给ostream_iterators写一个例子。下面是一个单词副本的单词副本,但我无法在VS2015上编译它而没有错误,我不明白错误消息
std::ostream_iterator<string> oo { cout };
*oo2 = "Hello, "; // compile error, see below
++oo2;
我也试过
*oo2 = string("Hello"); // same error
非常长的错误消息的开头是
1>c:\program files (x86)\microsoft visual studio 14.0\vc\include\iterator(317): error C2679: binary '<<': no operator found which takes a right-hand operand of type 'const std::string' (or there is no acceptable conversion)
1> c:\program files (x86)\microsoft visual studio 14.0\vc\include\ostream(495): note: could be 'std::basic_ostream<char,std::char_traits<char>> &std::basic_ostream<char,std::char_traits<char>>::operator <<(std::basic_streambuf<char,std::char_traits<char>> *)'
<...more...>
使用char *的不同版本可以正常工作:
// works
std::ostream_iterator<const char *> oo5{ std::cout }; //const or no const, either works
*oo5 = "hello, ";
++oo5;
*oo5 = " world\n";
字符串版本有什么问题?这是书中的错误还是我的错误?感谢
修改 我还要注意:
std::ostream_iterator<char> oo{ std::cout };
string s{ "hello, stream_iterator<char>, copy string" };
std::copy(s.begin(), s.end(), oo);
作品
答案 0 :(得分:-1)
您的代码有拼写错误。这有效吗?
#include <iostream>
#include <ostream>
#include <string>
int main()
{
std::ostream_iterator<std::string> oo2{ std::cout };
*oo2 = "Hello, ";
++oo2;
return 0;
}
我得到了这个输出:
您好,