C :Bool方法在COUT语句之后返回意外数字
C++: bool method returning unexpected numbers after cout statement
我正在为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"输出。
相关文章:
- 了解在返回值之前和之后使用 EAX 的函数调用
- kill() 总是返回 0(成功),即使在子进程已经结束之后?
- 为什么我的类工作正常,即使在返回垃圾值作为赋值运算符和空复制构造函数的返回之后
- 如果语句之后,返回vs无能为力
- 移动了 QT 项目,重命名文件夹和项目名称,Moc'ing 返回"'-I'之后缺少值"
- C :Bool方法在COUT语句之后返回意外数字
- 可以在 main 的返回 0 语句之后调用任何函数吗?
- 为什么 queue:front() 的返回值在 queue::p op() 之后有效
- 返回从字符串末尾 (C++) 之后的位置开始的子字符串
- 我的程序返回整数值,但即使在显式类型转换之后也应该给出双值
- 为什么QDrag::target()在QDrag::exec()之后返回一个奇怪的地址
- std::queue::size() 可以在 size() == 0 的 pop() 之后返回一个巨大的数字
- 欧拉项目任务#8,代码在某个点之后开始返回错误的答案
- C++函数中返回语句之后的指令
- 在计算函数返回值之前或之后是否销毁了局部变量
- Strcat()在strcpy之后返回垃圾
- 如何在SIGTSTP之后返回shell
- 在自动销毁变量之前或之后创建的c++返回值
- 封顶libcurl在X字符之后返回
- GetIndices()会在CreateIndexBuffer()和SetIndices()之后返回相同的缓冲区指针吗?