开发者

cerr is undefined

开发者 https://www.devze.com 2023-01-23 03:29 出处:网络
I have some problems, I\'m getting these errors (marked in the code): identifier \"cerr\" is undefined

I have some problems, I'm getting these errors (marked in the code):

  • identifier "cerr" is undefined
  • no operator "<<" matches these operands

Why?

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

using namespace std;

int main()
{

    ofstream output("output.txt",ios::ou开发者_Go百科t);
    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
}


You must include iostream in order to use cerr.
See http://en.cppreference.com/w/cpp/io/basic_ostream.


You need to add this at the top :

#include <iostream>

for cerr and endl


include iostream for cerr support.

And there is no implementation of operator << for class Basic. You'd have to make that implementation yourself. See here.


#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;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消