C++ 中的 ostream 使用
我有一个程序显示错误。如何解决错误并使用 ostream 显示输出 我在 ubuntu 中使用 g++ 编译器
#include<iostream>
using namespace std;
int main()
{
ostream out;
out<<"Hello World";
}
I have a program showing error. How to resolve the error and to use ostream to display output
I use g++ compiler in my ubuntu
#include<iostream>
using namespace std;
int main()
{
ostream out;
out<<"Hello World";
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您想要的 ostream(附加到显示器)已被定义为
cout
。并非所有 ostream 都将流发送到终端显示器。
The ostream that you want (attached to the display) has already been defined as
cout
.Not all
ostream
s send the stream to the terminal display.std::ostream
没有默认构造函数, this:将出现编译时错误。
您可能想要使用 std::cout (如前所述)。
std::ostream
does not have a default constructor, this:will be a compile time error.
You are probably wanting to use
std::cout
(as has already been stated).首先,包含
#include
。其次,将ofstream out
更改为ofstream out("file.txt")
。Firstly, include
#include <fstream>
. Secondly, changeofstream out
toofstream out("file.txt")
.为了进行一些输出,您需要获得正确的
ostream
。正如 Drew Dormann 向您展示的,您可以使用 std::cout 在标准输出上写入。您还可以使用std::cerr
作为标准错误,最后,如果您想要在文件上写入,您还可以实例化自己的fstream
。作为旁注:我建议不要在程序(请参阅此常见问题解答)
In order to do some output you need to get the right
ostream
. As Drew Dormann showed you, you can usestd::cout
for writing on standard output. You can also usestd::cerr
for the standard error, and finally you can instantiate your ownfstream
if you want, for instance, to write on a file.As side note: i suggest not to export the
std
namespace (use namespace std
) in your programs (see this faq)