使用指定给ostream_迭代器* Stroustrup的第107页的C++编程语言(第四ED)是向OrthyType迭代器分配给它们的一个例子。下面是示例子集的逐字副本,但我无法在VS2015上编译它,而且我不理解错误消息 std::ostream_iterator<string> oo { cout }; *oo2 = "Hello, "; // compile error, see below ++oo2;
下面是非常长的错误消息的开头使用指定给ostream_迭代器* Stroustrup的第107页的C++编程语言(第四ED)是向OrthyType迭代器分配给它们的一个例子。下面是示例子集的逐字副本,但我无法在VS2015上编译它,而且我不理解错误消息 std::ostream_iterator<string> oo { cout }; *oo2 = "Hello, "; // compile error, see below ++oo2;,c++,visual-studio,visual-studio-2015,C++,Visual Studio,Visual Studio 2015,下面是非常长的错误消息的开头 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&
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...>
1>c:\ProgramFiles(x86)\microsoft visual studio 14.0\vc\include\iterator(317):错误C2679:binary'您的代码有输入错误。这行得通吗
#include <iostream>
#include <ostream>
#include <string>
int main()
{
std::ostream_iterator<std::string> oo2{ std::cout };
*oo2 = "Hello, ";
++oo2;
return 0;
}
#包括
#包括
#包括
int main()
{
std::ostream_迭代器oo2{std::cout};
*oo2=“你好,”;
++oo2;
返回0;
}
我得到这个输出:
您好,您是否包含了
?这就是问题所在!谢谢在#include之前,我实际上在几个地方使用了字符串,没有发生任何意外。我不明白为什么会这样做,但确实如此。string类被标准库的一些其他部分使用,因此它有时通过一些特定于实现的头引入,而这个头不一定会带来
头中的所有内容(例如操作符)
// 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);
#include <iostream>
#include <ostream>
#include <string>
int main()
{
std::ostream_iterator<std::string> oo2{ std::cout };
*oo2 = "Hello, ";
++oo2;
return 0;
}