@@~. Tìm nguyên nhân hay ghê nhỉ? Code sai chỗ này nè, đưa vào danh sách địa chỉ của 1 đối tượng cục bộ, sau khi ra khỏi khối lệnh, đối tượng bị giải phóng mất tiêu lấy gì mà gọi hàm???
Sửa lại như sau:C++ Code:
if(string(node->name()) == "rect")//Mỗi một node gồm có 2 thuộc tính : tên và giá trị { Reca temp; for(xml_attribute<> *attr = node->first_attribute(); attr;attr = attr->next_attribute())//duyệt hết tất cả các thuộc tính { temp.SetAttribute(attr->name(),attr->value()); } Figure* temp_fig; temp_fig = &temp; this->figure.push_back(temp_fig); } if(string(node->name()) == "circle") { Circle temp; for(xml_attribute<> *attr = node->first_attribute(); attr;attr = attr->next_attribute()) { temp.SetAttribute(attr->name(),attr->value()); } Figure* temp_fig; temp_fig = &temp; this->figure.push_back(temp_fig); } if(string(node->name()) == "line") { Line temp; for(xml_attribute<> *attr = node->first_attribute(); attr;attr = attr->next_attribute()) { temp.SetAttribute(attr->name(),attr->value()); } Figure* temp_fig; temp_fig = &temp; this->figure.push_back(temp_fig); } if(string(node->name()) == "ellipse") { Elli temp; for(xml_attribute<> *attr = node->first_attribute(); attr;attr = attr->next_attribute()) { temp.SetAttribute(attr->name(),attr->value()); } Figure* temp_fig; temp_fig = &temp; this->figure.push_back(temp_fig); } if(string(node->name()) == "polyline") { Poly temp; for(xml_attribute<> *attr = node->first_attribute(); attr;attr = attr->next_attribute()) { temp.SetAttribute(attr->name(),attr->value()); } Figure* temp_fig; temp_fig = &temp; this->figure.push_back(temp_fig); } if(string(node->name()) == "polygon") { Polyg temp; for(xml_attribute<> *attr = node->first_attribute(); attr;attr = attr->next_attribute()) { temp.SetAttribute(attr->name(),attr->value()); } Figure* temp_fig; temp_fig = &temp; this->figure.push_back(temp_fig); }
P/s 1: Vì tạo đối tượng bằng toán tử new, nên cần bổ sung toán tử delete để giải phóng đối tượng. Cái này cài tuỳ theo yêu cầu của bạn.C++ Code:
if(string(node->name()) == "rect")//Mỗi một node gồm có 2 thuộc tính : tên và giá trị { Figure* temp_fig = new Reca; for(xml_attribute<> *attr = node->first_attribute(); attr;attr = attr->next_attribute())//duyệt hết tất cả các thuộc tính { temp_fig->SetAttribute(attr->name(),attr->value()); } this->figure.push_back(temp_fig); } if(string(node->name()) == "circle") { Figure* temp_fig = new Circle; for(xml_attribute<> *attr = node->first_attribute(); attr;attr = attr->next_attribute()) { temp_fig->SetAttribute(attr->name(),attr->value()); } this->figure.push_back(temp_fig); } if(string(node->name()) == "line") { Figure* temp_fig = new Line; for(xml_attribute<> *attr = node->first_attribute(); attr;attr = attr->next_attribute()) { temp_fig->SetAttribute(attr->name(),attr->value()); } this->figure.push_back(temp_fig); } if(string(node->name()) == "ellipse") { Figure* temp_fig = new Elli; for(xml_attribute<> *attr = node->first_attribute(); attr;attr = attr->next_attribute()) { temp_fig->SetAttribute(attr->name(),attr->value()); } this->figure.push_back(temp_fig); } if(string(node->name()) == "polyline") { Figure* temp_fig = new Poly; for(xml_attribute<> *attr = node->first_attribute(); attr;attr = attr->next_attribute()) { temp_fig->SetAttribute(attr->name(),attr->value()); } this->figure.push_back(temp_fig); } if(string(node->name()) == "polygon") { Figure* temp_fig = new Polyg; for(xml_attribute<> *attr = node->first_attribute(); attr;attr = attr->next_attribute()) { temp_fig->SetAttribute(attr->name(),attr->value()); } this->figure.push_back(temp_fig); }
P/s 2: hàm "SVG::SVG(const SVG& svg)" có vấn đề, xem lại cách dùng con trỏ đi. Còn lỗi linh tinh ở vài chỗ nữa. ~>"<~ Lắm bug thế không biết!