Answer the question
In order to leave comments, you need to log in
Why can't I add an object to a vector?
You need to add objects of different classes that are descendants of the base class to the hierarchy tree.
An exception was thrown at 0x65DA8871 (vcruntime140d.dll) in Lab1.exe: 0xC0000005: Access violation while reading at address 0xCB000024.
void rootObject::build()
{
cin >> head_name;
base primary(head_name, 1);
base* inner_component = nullptr;
cin >> head_name >> component_name >> classNumber >> status;
while (true) {
if (head_name == primary.get_name()) {
if (classNumber == 2)
primary.add(new object2(component_name, status, &primary));
if (classNumber == 3)
primary.add(new object3(component_name, status, &primary));
if (classNumber == 4)
primary.add(new object4(component_name, status, &primary));
}
else {
if (classNumber == 2)
inner_component = dynamic_cast <object2*>(primary.find(head_name));
if (classNumber == 3)
inner_component = dynamic_cast <object3*>(primary.find(head_name));
if (classNumber == 4)
inner_component = dynamic_cast <object4*>(primary.find(head_name));
if (inner_component != nullptr) {
if (classNumber == 2)
(*inner_component).add(new object2(component_name, status, &primary));
if (classNumber == 3)
(*inner_component).add(new object3(component_name, status, &primary));
if (classNumber == 4)
(*inner_component).add(new object4(component_name, status, &primary));
}
}
cin >> head_name;
if (head_name == "endtree")
break;
cin >> component_name >> classNumber >> status;
}
cout << "Test result";
primary.print_status();
}
void base::add(base* component) {
components.push_back(component);
}
vector <base*> components;
Answer the question
In order to leave comments, you need to log in
You have a strange find() function:
base* base::find(string name) {
base* object = nullptr;
for (base* element : this->components) {
if (element->get_name() == name)
object = element;
if (object == nullptr) {
object = element->find(name);
}
return object;
}
}
if (classNumber == 2)
inner_component = dynamic_cast <object2*>(primary.find(head_name));
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question