Cerr undefined

I have some problems, I get these errors (noted in the code):

  • Identifier "cerr" undefined
  • no operator "<matches these operands

Why?

#include "basic.h"
#include <fstream>

using namespace std;

int main()
{

    ofstream output("output.txt",ios::out);
    if (output == NULL)
    {
        cerr << "File cannot be opened" << endl;   // first error here
        return 1;
    }

    output << "Opening of basic account with a 100 Pound deposit: "
        << endl;
    Basic myBasic (100);
    output << myBasic << endl;   // second error here
}
+5
source share
4 answers

You must enable iostreamto use cerr.
See http://en.cppreference.com/w/cpp/io/basic_ostream .

+17
source

You need to add this at the top:

#include <iostream>

for cerr and endl

+9
source

iostream cerr.

< < Basic. . . .

+9
#include <fstream>
#include <iostream>

#include "basic.h"


std::ostream& operator<<(std::ostream &out, Basic const &x) {
  // output stuff: out << x.whatever;
  return out;
}

int main() {
  using namespace std;

  ofstream output ("output.txt", ios::out);
  if (!output) {  // NOT comparing against NULL
    cerr << "File cannot be opened.\n";
    return 1;
  }

  output << "Opening of basic account with a 100 Pound deposit:\n";
  Basic myBasic (100);
  output << myBasic << endl;

  return 0;
}
+2

All Articles