我不明白为什么我在向地图的引用向量添加元素时遇到问题(即有时它会起作用,有时会失败)。
例如:
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();