There is a class in which there is a vector field from unique_ptr ( Animals is also a class), as well as methods of reading and writing to the file of this class vector. Here is the code snippet:

 class Hendler { private: char* input; char* output; std::vector <std::unique_ptr <ModelObject>> * Animals; std::vector <std::unique_ptr <Event>> * E; public: Hendler(char* in, char* out, std::vector <std::unique_ptr <ModelObject> > * A, std::vector <std::unique_ptr <Event> > * Ev) : input(in), output(out), Animals(A), E(Ev) {}; void inputing() { std::fstream inputFile; inputFile.open(input); coord p, v; std::vector <std::unique_ptr <ModelObject> > Animals; // some code for (int i = 1; i <= D; i++) { inputFile >> px >> py >> pz >> vx >> vy >> vz; Animals.emplace_back(new Dragonfly(p, v, DS)); } inputFile.close(); } void outputing() { //some code for (int i = 0; i < Animals->size(); ++i) { switch ((*Animals)[i]->get_type()) { case dragonfly: out << (*Animals)[i]->get_position().x << ' ' << (*Animals)[i]->get_position().y << ' ' << (*Animals)[i]->get_position().z << ' ' << (*Animals)[i]->get_vector().x << ' ' << (*Animals)[i]->get_vector().y << ' ' << (*Animals)[i]->get_vector().z << ' ' << '\n'; break; //some code }; int main(int argc, char ** argv) { std::vector <std::unique_ptr <ModelObject>> Animals; std::vector <std::unique_ptr <Event> > Events; Hendler HendlerFile(argv[1],argv[2], &Animals, &Events); HendlerFile.inputing(); //Model Mod(&Animals, &Events); //HendlerFile.set_N(Mod.modulating(HendlerFile.get_N())); HendlerFile.outputing(); return 0; } 

But after doing Maine, the output file is proust ... I understand that the transfer to the Handler class of the vector is not correct, but how to do it correctly - I won’t put my mind on it .. З.Ы. the vector from main should change.

  • one
    Try a minimal reproducible example . In its current form, your problem is not entirely clear. - αλεχολυτ
  • I did not have time to give an answer, we were ahead, so I will give a comment - do not do these tricks with get_type() and high-rise switch . That is why classes and inheritance were invented, so that this should not be fenced off ... - Harry

2 answers 2

This vector declaration in the inputing class member inputing

 void inputing() { std::fstream inputFile; inputFile.open(input); coord p, v; std::vector <std::unique_ptr <ModelObject> > Animals; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 

is a local variable declaration for which the destructor will be called when the function terminates. The data member of the Animals class itself, declared as

 std::vector <std::unique_ptr <ModelObject>> * Animals; 

it does not change at all.

In addition, it is completely incomprehensible why you declared this data member as a pointer to a vector instead of the vector object itself.

    In the inputting function, you have a local vector in which the changes are saved, and after exiting the function it collapses.

    • Thank you very much)) Stupidly somehow turned out .. - Dmitriy1996