2010-11-01 47 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