-3

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";
}
ismail
  • 46,010
  • 9
  • 86
  • 95
smartkid
  • 1,499
  • 3
  • 14
  • 23

4 Answers4

4

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 ostreams send the stream to the terminal display.

Drew Dormann
  • 59,987
  • 13
  • 123
  • 180
2

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).

hmjd
  • 120,187
  • 20
  • 207
  • 252
2

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;
}
Software_Designer
  • 8,490
  • 3
  • 24
  • 28
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)

Dacav
  • 13,590
  • 11
  • 60
  • 87