Hello, could you explain to me what is my mistake (or not understanding) in this code.

I have a class tree that the designer has

AVL_Tree<Key, mapped_type, Compare>::AVL_Tree(std::string dir) { root = lastAction = nullptr; dirName = dir; count = 0; Key key=10; mapped_type value = 11; insert(std::make_pair(key, value)); }; 

A tree node is a structure.

 struct Tree_ { Key key; std::string* addr; unsigned char height; Tree_* parent; Tree_* left; Tree_* right; Tree_(Key k, std::string* p) { key = k; addr = p; left = right = parent = 0; height = 1; } } 

when insert, the position of the node is relative to the root and a new object of the node is inserted (I think there is no sense in showing the insert code) I insert the code in this way

 p = new Tree_(k.first,&dirName); 

The problem is that when creating an instance of a tree:

 typedef AVL_Tree<Key, FileType> tree; tree strom2; strom2 = (dirName); 

dirName not saved in my nodes, but if I write this:

 tree strom2 = (dirName); 

that persists.

Can you tell what the reason is and how to solve it?

    1 answer 1

    Let's take a look at both options and how they differ.
    Here you call a constructor, the resulting object is saved into a variable:

     tree strom2 = (dirName); 

    And here you first create a variable without explicitly specifying a value — the default constructor is invoked (which is not present, but here it does not matter).

     tree strom2; 

    Then, as I understand it, a new object is created by the constructor with the parameter. And it is assigned to that variable. Since the object is passed by value, not by reference, an assignment operator is called, which you also did not define:

     strom2 = (dirName); 

    Hence the solution: reload the assignment operator .

    UPD. You almost correctly wrote the declaration of the harassment operator, you need to add more template parameters. I suggest you call the copy constructor in it :

     /// Assignment operator. AVL_Tree<Key,mapped_type,Compare>& operator=(const AVL_Tree<Key,mapped_type,Compare> &oth) { // на всякий случай if (this == &oth) return *this; return AVL_Tree<Key,mapped_type,Compare>(oth); } /// Copy constructor. AVL_Tree(AVL_Tree const &oth) { // здесь копируйте нужные данные this->abc = oth.abc; } 

    tree strom2 = new tree(dirName); It does not work at all, because new returns a pointer to the created object, you have a variable type not a pointer. And in principle, when there is no need, pointers should not be used, so as not to forget to clear the memory.


    UPD2.

    I checked the code, also got not quite the desired result. In general, you can do without a copy constructor. Since the assignment operator is not static, we need to work with the values ​​of the current object. And the return value is a reference to it for the possibility of such an entry: a = b = c;

     AVL_Tree<Key,mapped_type,Compare>& operator=(const AVL_Tree<Key,mapped_type,Compare> &oth) { if (this == &oth) return *this; // копирование данных this->abc = oth.abc; return *this; } 
    • I understood correctly, do I need to write the AVL_Tree& operator= (const AVL_Tree& other) in the tree? and then pass all other variables to the left? But then what to do with pointers? I use them to build the root and its children. - Demolver
    • Yes, with copying the necessary data. - AivanF.
    • copying links or creating new objects with the same values? and another question, which is more desirable, use tree strom2 = (dirName); or tree strom2 = new tree(dirName); - Demolver
    • @Demolver will now add the answer. - AivanF.
    • @Demolver take a look. - AivanF.