I
I
Ilya2020-11-27 23:59:36
C++ / C#
Ilya, 2020-11-27 23:59:36

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.

Program execution example

Ввод:
root (Корневой объект)
root obj1 3 5 (Головной объект, подчиненный объект, номер класса объекта, состояние объекта)
root obj2 4 -5
root obj3 2 1
obj3 obj4 2 5

Вывод:
Test result
The object root is ready.
The object obj1 is ready.
The object obj2 is not ready.
The object obj3 is ready.
The object obj4 is ready.

The error is as follows: the line obj3 obj4 2 5 is not executed, an error is thrown:
An exception was thrown at 0x65DA8871 (vcruntime140d.dll) in Lab1.exe: 0xC0000005: Access violation while reading at address 0xCB000024.

Moreover, it is possible to add to the root object without errors. The only time the program fails is when I try to add an object that is subordinate to a non-root object.
Here is the code of the hierarchy tree building function:
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);
}

The vector itself:
vector <base*> components;
Link to the entire project if it helps with solving the problem: https://dropmefiles.com/uJkqo

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
galaxy, 2020-11-28
@Avlordin

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;
  }
}

It doesn't look past the first components element. obj3 it does not find, respectively, the program crashes on casting nullptr to object2
if (classNumber == 2)
        inner_component = dynamic_cast <object2*>(primary.find(head_name));

(I don't understand why there are so many castes)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question