C ++函数重载导致seg错误

我有一个名为Set的结构。

struct Set {
string name;
string value;
bool is_condition;
bool is_bool_val; };

我必须存储许多这些“集合”结构,因此我正在使用向量。

vector<Set> list;

就上下文而言,我存储的是有关移动设备(如iPhone)的数据。Set的列表描述了一种设备!例如,set的实例可以具有以下内容...

Set phone;
phone.name = "Serial Number";
phone.value = "FLMJ0000GGGG";
phone.is_condition = false;
phone.is_bool = false;

is_condition,告诉我Set的此实例需要存储“ Good”或“ Bad”作为值。is_bool,告诉我Set的实例需要存储一个布尔值。请参见以下示例。

Set device_wiped;
device_wiped.name = "Device Wiped";
device_wiped.is_bool = true;

从is_bool推断可以让我获得true或false的输入,这意味着该设备已擦除或尚未擦除(恢复为出厂设置)。

我正在使用名为new_set的重载包装函数来创建我的Set元素,这些元素存储在我的“列表”向量中。

Set new_set(string name, const bool is_bool, const bool is_condition) {
    Set set;
    set.name = name;
    set.is_bool = is_bool;
    set.is_condition = is_condition;
    return set;
}

Set new_set(string name, const bool is_bool) {
    return new_set(name, is_bool);
}

Set new_set(string name) {
    return new_set(name, false, false);
}

我有三个包装函数,因为这使实现起来更容易(我只想在必要时编写参数!)。这样编译可以,但是我不能运行。分段错误崩溃。让我知道您是否需要完整的代码。

现在,我不需要将数据存储在Set.value或Set.condition字段中。我只想使用布尔值来初始化它们。

汉0508

问题出在第二个函数中:

Set new_set(string name, const bool is_bool) {
    return new_set(name, is_bool);
}

这里有无限的递归。我想你是说return new_set(name, is_bool, false);还是return new_set(name, is_bool, true);

顺便说一下,您的三个功能可以简化为一个(如下所示)(如@SirGuy所述):

Set new_set(string name, const bool is_bool = false, const bool is_condition = false) {
    Set set;
    set.name = name;
    set.is_bool_val = is_bool;
    set.is_condition = is_condition;
    return set;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章