2

编辑:忘了说我的编程语言是 C++,我的 IDE 是 Qt Creator。

我不明白为什么我在向地图的引用矢量添加元素时遇到问题(即有时它有效,有时它失败)。

例如:

class C_html4_tags {
     public:
       C_html4_tags();
       //.......

       typedef std::vector<S_html_attr_value> TYPE_attr_values;

    //.....
};

//...
C_html4_tags::C_html4_tags() {

//........
map<S_browser, TYPE_attr_values> attr_supported_attr_values_map;
S_browser dummy_browser; //a fake browser key for referencing a fully Html 4.01-compliant supported attr values list
dummy_browser.name = "Dummy";

//.....
TYPE_attr_values& attr_supported_attr_values = attr_supported_attr_values_map[dummy_browser];

//......
**attr_supported_attr_values = attr_supported_attr_values_map[dummy_browser];**

//...
**attr_supported_attr_values.clear();
  attr_supported_attr_values_map.clear();**

//......
**attr_supported_attr_values = attr_supported_attr_values_map[dummy_browser];**

//...
**attr_supported_attr_values.clear();
attr_supported_attr_values_map.clear();**

}

我用一堆不同的属性多次使用粗体线,它们之间几乎没有区别,直到达到这个属性(a_tabindex_attr)之前没有问题,其中IDE在正常运行时不会报告任何错误(除了“程序意外完成。” 但是,在调试它时,它现在报告:

收到信号

下级停止了,因为它收到了来自操作系统的信号。

信号名称:SIGSEGV 信号含义:分段错误

回溯指向我上面提到的属性,在执行此操作的代码行上:

attr_supported_attr_values.clear();

现在,在向代码中添加了一些调试 cout 行之后,我了解到,出于某种原因,即使在执行以下操作之后也会发生以下情况:

attr_supported_attr_values.push_back(attr_value);

向量对象,它是返回的映射值:

attr_supported_attr_values = attr_supported_attr_values_map[dummy_browser];

当我将 S_html_attr_value 对象 push_back 到引用的向量时,实际上并没有改变。现在,我不知道为什么会这样,因为我在这个之前的一堆其他属性的代码中做了几乎完全相同的事情,没有任何问题,但现在出于某种原因,在这个上,它没有添加attr_supported_attr_values 的对象(这是对返回的“dummy_browser”映射值的引用)。我知道这是一个事实,因为我输出了映射的内部映射值对象的大小(使用映射的迭代器),在调用 push_back() 后它为 0。然而,更奇怪的是,在调用 push_back() 之后,我还输出了 attr_supported_attr_values.size(),而且是 1!现在怎么可能,因为它们都应该是同一个对象?

笔记:

我正在谈论的完整代码如下(或者至少是属性代码,减去调试语句......):

//a_tabindex_attr:
attr_desc = "Specifies the position of an <a> element in the\n"
            "tabbing order for the current document.\n"
            "The tabbing order defines the order in which\n"
            "elements will receive focus when navigated by\n"
            "the user via the keyboard. The tabbing order\n"
            "may include elements nested within other elements.\n"
            "Elements that may receive focus based on tabindex\n"
            "adhere to the following rules:\n\n"
            "1. Those elements that support this attribute and\n"
            "assign a positive value to it are navigated first.\n"
            "Navigation proceeds from the element with the\n"
            "lowest tabindex value to the element with the\n"
            "highest value. Values need not be sequential\n"
            "nor must they begin with any particular value.\n"
            "Elements that have identical tabindex values\n"
            "should be navigated in the order they appear\n"
            "in the character stream.\n"
            "2. Those elements that do not support this\n"
            "attribute or support it and assign it a value\n"
            "of \"0\" are navigated next. These elements are\n"
            "navigated in the order they appear in the\n"
            "character stream.\n"
            "3. Elements that are disabled do not participate\n"
            "in the tabbing order.";
attr_supported_attr_values = attr_supported_attr_values_map[dummy_browser];

attr_value_desc = "A number you specify for the tab index/order.\n"
                  "It must be a value between 0 and 32767.";
attr_value_content = "<i>number</i>";
attr_value = C_html4_attributes_obj.getAttrValue(attr_value_desc, attr_value_content,
                                                 attr_value_supported_browsers,
                                                 attr_value_required, attr_value_deprecated,
                                                 attr_value_notes, attr_value_tips);
attr_value.is_relative = true;
attr_supported_attr_values.push_back(attr_value);

try {
   N_init_class_members::initAttr(C_html4_attributes::attr_tabindex, a_tabindex_attr, attr_desc,
                                  attr_supported_browsers, attr_supported_attr_values_map, attr_required,
                                  attr_deprecated, attr_notes, attr_tips);
}

catch (const char* exc) {
     string exc_str = "Error! Call to N_init_class_members::initAttr() from\n"
                      "constructor of C_html4_tags class. That function\n"
                      "reported the following exception:\n\n";
     exc_str += exc;
     throw (exc_str.c_str()); //re-throw exception
}
a_tabindex_attr.is_standard_attr = true;
a_tabindex_attr.is_optional_attr = true;

//cleanup from attribute operations so we can begin working on the next attribute:
C_html4_attributes_obj.clear(); //wipes the slate clean for all the supported attributes' properties except content
attr_desc.clear();
attr_supported_attr_values.clear();
attr_supported_attr_values_map.clear();
attr_value_desc.clear();
attr_value_content.clear();
4

0 回答 0