2010-11-01 44 views
5

我有一些问题,我得到这些错误(标记代码):CERR是不确定的

  • 标识符“CERR”未定义
  • 没有运营商“< <”匹配这些操作数

为什么?

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

回答

9

你需要在顶部加入这样的:

#include <iostream> 

为CERR和ENDL

9

包括用于iostream的支持CERR。

对于Basic类没有实现运算符< <。你必须自己实现这个实现。看到here.

2
#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

不错,但国际海事组织设置fstream在出现错误的情况下抛出异常更好。 – 2010-11-01 13:44:36