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";
}
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";
}
The ostream that you want (attached to the display) has already been defined as cout
.
#include<iostream>
using namespace std;
int main()
{
cout<<"Hello World";
}
Not all ostream
s send the stream to the terminal display.
std::ostream
does not have a default constructor, this:
ostream out;
will be a compile time error.
You are probably wanting to use std::cout
(as has already been stated).
Firstly, include #include <fstream>
. Secondly, change ofstream out
to ofstream out("file.txt")
.
#include <iostream>
#include <fstream>
using namespace std;
int main () {
ofstream out ("c:\\test5.txt");
out<<"Hello World";
out.close();
return 0;
}
In order to do some output you need to get the right ostream
. As Drew Dormann showed you, you can use std::cout
for writing on standard output. You can also use std::cerr
for the standard error, and finally you can instantiate your own fstream
if you want, for instance, to write on a file.
#include <iostream>
#include <fstream>
int main()
{
std::fstream outfile ("output.txt", fstream::out);
outfile << "Hello World" << std::endl;
// Always close streams
outfile.close();
}
As side note: i suggest not to export the std
namespace (use namespace std
) in your programs (see this faq)