c ++中的cerr和cout意外输出

时间:2018-02-26 13:20:43

标签: c++

我定义了两个类:

#include <iostream>
#include <vector>
#include <string>
#include <stdexcept>
#include <algorithm>
#include <cmath>
#include <list>
using namespace std;


vector<string> zustand_namen = {"Neuwertig","Gut", "Abgegriffen","Unbrauchbar"};

enum class Zustand{Neuwertig,Gut, Abgegriffen,Unbrauchbar};

class Exemplar{
private:
    int num;
    Zustand zust;
    bool verliehen;
public:
    Exemplar(int num,Zustand zust);
    Exemplar(int num);
    bool verfuegbar() const;
    bool entleihen();
    void retournieren(Zustand z);
    friend ostream& operator<<(ostream& os, const Exemplar& ex);
};


class Werk{
private:
    string autor;
    string name;
    int jahr;
    vector<Exemplar> vec;
public:
    Werk(string autor,string name,int jahr);
    void erwerben(int nr, Zustand z);
    bool entleihen();
    void retournieren(int index, Zustand z);
    friend ostream& operator<<(ostream& os, const Werk& werk);

};

我在主程序中测试了我的结果:

int main() {
  try {
    Exemplar e{0, Zustand::Unbrauchbar};
    cout << e << '\n';
  }
  catch (runtime_error&) {
    cout << "Error1\n";
  }
  try {
    Exemplar e{1};
    cout << e << '\n';
    cerr << e << '\n';
  }
  catch (runtime_error&) {
    cout << "Error2\n";
  }
  try {
    Exemplar e{1001};
    cout << e << '\n';
  }
  catch (runtime_error&) {
    cout<<"Error3\n";
  }



  return 0;
}

我期待输出:

Error1
[Auflage: 1, Zustand: neuwertig]
[Auflage: 1, Zustand: neuwertig]
Error3
[Auflage: 2, Zustand: gut]
[Auflage: 2, Zustand: gut]

但相反,我得到了:

Error1
[Auflage:1, Zustand:Neuwertig]
Error3
[Auflage:1, Zustand:Neuwertig]

为什么会这样?这个输出的原因是什么(我想它应该与cerr函数连接,如何修复它,以便获得第一个输出?任何更正和解释都将受到赞赏

P.S我的建设者:

Werk::Werk(string autor,string name,int jahr):
    autor{autor},
    name{name},
    jahr{jahr}
    {
        if(autor.size()<1) throw runtime_error("autor is incorrect");
        if(name.size()<1) throw runtime_error("name is incorrect");
        if(jahr<1700 ||jahr >2017) throw runtime_error("jahr is incorrect");
    }


Exemplar::Exemplar(int num,Zustand zust):
    num{num},
    zust{zust},
    verliehen{false}
    {
        if(num <1 || num >1000) throw runtime_error("num is incorrect");
    }



Exemplar::Exemplar(int num):
    num{num},
    zust{Zustand::Neuwertig},
    verliehen{false}
    {
        if(num <1 || num >1000) throw runtime_error("num is incorrect");
    }

输出运营商:

ostream& operator<<(ostream& os, const Exemplar& ex){
    if(ex.verliehen == true){
        os << "[" << "Auflage:" << ex.num <<", " << "Zustand:" << zustand_namen[static_cast<int>(ex.zust)] <<", " << "verliehen"<<"]";
    }
        os << "[" << "Auflage:" << ex.num <<", " << "Zustand:" << zustand_namen[static_cast<int>(ex.zust)] << "]";
    return os;
}

ostream& operator<<(ostream& os, const Werk& werk){
    os << "[" << werk.autor << ", " << werk.name << ", " << werk.jahr << "{";
    for(int i = 0; i<werk.vec.size();++i){
        if(i < werk.vec.size()-1){
            os << werk.vec[i] << ", ";
        } else{
            os << werk.vec[i];
        }

    }
    return os;
}

1 个答案:

答案 0 :(得分:1)

  

我期待输出:

Error1
[Auflage: 1, Zustand: neuwertig]
[Auflage: 1, Zustand: neuwertig]
Error3
[Auflage: 2, Zustand: gut]
[Auflage: 2, Zustand: gut]
     

但相反,我得到了:

Error1
[Auflage:1, Zustand:Neuwertig]
Error3
[Auflage:1, Zustand:Neuwertig]

第一个差异资本版本neuwertigNeuwertig是因为

vector<string> zustand_namen = {"Neuwertig","Gut", "Abgegriffen","Unbrauchbar"};

第二个区别,一个输出线对两条线,是因为输出运算符

    if (ex.verliehen == true) {
        os << "[" << "Auflage:" << ex.num <<", " << "Zustand:" << zustand_namen[static_cast<int>(ex.zust)] <<", " << "verliehen"<<"]";
    }

    os << "[" << "Auflage:" << ex.num <<", " << "Zustand:" << zustand_namen[static_cast<int>(ex.zust)] << "]";

当成员verliehen设置为true时,将打印两行。如果成员为false,则只打印一行。成员verliehen在构造函数中初始化为false并且从未更改过,因此条件永远不会为真且第一行从未打印过。