C :Bool方法在COUT语句之后返回意外数字

C++: bool method returning unexpected numbers after cout statement

本文关键字:之后 返回 意外 数字 语句 COUT Bool 方法      更新时间:2023-10-16

我正在为C 编程考试进行一些考试审查,以及我坚持使用的示例,需要我编码一类以返回"壁橱"对象的内容。示例中所需的一种方法之一将服装对象的向量添加到服装对象的第二个矢量中(因此,用衣服填充壁橱)。到目前为止在这里仅显示我获得意外回报的部分。

我认为其中一种构造函数甚至另一种方法可能会干扰输出,因此我跑了几个版本的代码槽A Visualizer(C 的Python Tutor),但这并没有引发任何新的见解,也没有调用其他方法(如预期的),也没有从构造函数提示的其他输出。

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

enum class Color{Red, Blue, Gray, Yellow};
const std::vector<std::string> color_names{"red", "blue", "gray", "yellow"};
enum class Type{Pants, Blouse, Shirt, Skirt};
const std::vector<std::string> type_names{"pants", "blouse", "shirt", "skirt"};
class Garment {
  int preis;
  Color farbe;
  Type typ;

  public:
  //Konstruktor
  Garment (int p, Color f = Color::Gray, Type t = Type::Pants){
      this->preis = p;
      this->farbe = f;
      this->typ = t;
      //negativer Preis = exception
      if (p < 0){throw runtime_error("Preis kleiner als 0!");} }

int get_price() const{
return this->preis; }
    Type get_type() const{
    return this->typ; }
    bool has_color(Color f) const{}
    void deteriorate(int w){}
    int get_index_color() const{}
int get_index_type() const{}    

   friend ostream& operator<<(ostream& out, const Garment &g){
        //[40000 Cent, yellow blouse]
out << "[" << g.preis << " Cent, "<< color_names[g.get_index_color()] 
<< " " << type_names[g.get_index_type()];
        out << "]";
        return out;
    } 

};
class Closet {
size_t capacity;
vector<Garment> inventory;

public:
//Konstruktor Beginn
Closet (size_t c, vector<Garment> inv){
    this->capacity = c;
    this->inventory = inv;
if (capacity < 5 || capacity > 300){throw runtime_error ("Komplette Kapazitaet ueber oder unterschritten!");}

if (this->inventory.size() > this->capacity){throw runtime_error ("Relative kapazitaet ueberschritten");}

        vector<int>kleiderliste {0,0,0,0};
         for (auto x : inv){
              if (x.Garment::get_type() == Type::Pants){kleiderliste[0]++;}
              if (x.Garment::get_type() == Type::Blouse){kleiderliste[1]++;}
              if (x.Garment::get_type() == Type::Skirt){kleiderliste[2]++;}
             if (x.Garment::get_type() == Type::Shirt){kleiderliste[3]++;}
         }
       int zaehler = 0;
        for (auto y : kleiderliste){
              if (y != 0 ){zaehler++;}
         }
    if (zaehler <2){throw runtime_error("Nur mehr kleidungsstuecke eines typs im schrank");}

}

bool add(vector<Garment> v){
            if ((v.size() + this->inventory.size()) <= this->capacity){
            cerr << 1;
            this->inventory.insert(this->inventory.begin(),v.begin(),v.end());
            return true;
            }else{
            cerr << 0;
            return false;
            }
}
double mean_price() const{
}
friend ostream & operator<<(ostream &out,const Closet &c){
    out << "[" << c.capacity << ",{";
     for (auto x : c.inventory){
        out <<x;
     }
    out << "},";
    out << c.mean_price();
    out << "]";
    return out;
    }
};
int main(){
Garment pants{34500, Color::Blue, Type::Pants};
Garment blouse{12700, Color::Red, Type::Blouse};
const Garment shirt{2300, Color::Yellow, Type::Shirt};
Garment shirt2{23500, Color::Red, Type::Shirt};
Garment skirt{26600, Color::Gray, Type::Skirt};
Garment skirt2{4600, Color::Blue, Type::Skirt};

Closet closet {10, {skirt, blouse, shirt, pants, skirt}}; 
cout << closet.add({shirt2, skirt2}) << closet.add({blouse,skirt,pants}) << closet.add({}) << closet.add({pants}) << 'n';
 return 0; }       

此代码应该通过COUT产生以下输出:1110.壁橱::添加方法应该返回三次返回true,又一次返回false。

我实际上通过Cout&lt;&lt;实际上获得的返回值IS:0111要测试该代码是否可以执行我要输出的1 for True 1,而CERR频道上的False也为0,在那里我得到了正确的1110号码。

是什么导致回报输出不是1110?方法是在编译器中以不同的顺序进行的调用吗?

如Raymond-chen所解释的那样,该方法不能在左至右顺序中调用,从而产生预期的" 1110"输出。不同的编译器导致执行呼叫的不同顺序。在此特定情况下,切换到clang编译器产生了预期的" 1110"输出。