【发布时间】:2016-04-08 00:32:24
【问题描述】:
我正在用一些枚举类型实例化一个对象,并尝试根据这些枚举类型设置一些字符串成员。但是,当我在调试和单步执行时,用于设置字符串的开关会触发每种情况,并且每个字符串都会被设置为每种枚举类型的最后一种情况。
enum Number {
one,
two,
three
};
enum Color {
purple,
red,
green
};
enum Shading {
solid,
striped,
outlined
};
enum Shape {
oval,
squiggle,
diamond
};
Card::Card(Number num, Color colour, Shading shade, Shape shaper) {
number_ = num;
color_ = colour;
shading_ = shade;
shape_ = shaper;
setStrings();
}
void Card::setStrings() {
switch (number_) {
case one:
number_string = "one";
case two:
number_string = "two";
case three:
number_string = "three";
}
switch(color_) {
case purple:
color_string = "purple";
case red:
color_string = "red";
case green:
color_string = "green";
}
switch (shading_) {
case solid:
shading_string = "solid";
case striped:
shading_string = "striped";
case outlined:
shading_string = "outlined";
}
switch (shape_) {
case oval:
shape_string = "oval";
case squiggle:
shape_string = "squiggle";
case diamond:
shape_string = "diamond";
}
}
我使用重载构造函数实例化的每张卡片都有 number_string = "three"、color_string = "green"、shading_string = "outlined" 和 shape_string = "diamond"。
【问题讨论】:
标签: c++ visual-studio visual-c++ enums switch-statement