C++ cerr 未定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4069024/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 14:28:49  来源:igfitidea点击:

cerr is undefined

c++

提问by user195257

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

我有一些问题,我收到了这些错误(标记在代码中):

  • identifier "cerr" is undefined
  • no operator "<<" matches these operands
  • 标识符“cerr”未定义
  • 没有运算符“<<”匹配这些操作数

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
}

回答by Mauren

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

您必须包含iostream才能使用cerr.
请参阅http://en.cppreference.com/w/cpp/io/basic_ostream

回答by B?ови?

You need to add this at the top :

您需要在顶部添加:

#include <iostream>

for cerr and endl

对于 cerr 和 endl

回答by jilles de wit

include iostream for cerr support.

包括用于 cerr 支持的 iostream。

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

并且对于 Basic 类没有操作符 << 的实现。您必须自己实现该实现。看到这里。

回答by user195257

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