I get the error " no match for 'operator=' in '* n->node::child = root' in the "insert" method and I don't know how to fix it. I know it has something to do with pointers and the way I initialized the array. What I want to do is is put the 'root' in the parameter of the "insert" method in an array of type node. How can I do this? Could I get some pseudo-code please? I am at a loss. Don;t have much c++ experience.

struct node{
    /*
    struct node operator=(struct node root) {
     if(this == &root){
         return *this;
     }
    }
     */
    int numOfKeys;
    double* array;
    double* value;
    int count ;
    struct node *child;
    int i;
    int j;
};
/*
 * Node creator
 */
 
struct node* create (int keyNum){
            int size=2*keyNum;
            struct node* ptr;
            ptr->value=new double[size];
            ptr->child=new node [size];
            ptr->numOfKeys=0;
            ptr->i=0;
            ptr->j=ptr->i+1;
	    return ptr;
	};


FILE *txtFile;
node *root;
int numOfNodes=0;

struct node * insert ( int, struct node * ) ;
int setval ( int, struct node *, int *, struct node ** ) ;
struct node * search ( int, struct node *, int * ) ;
int searchnode ( int, struct node *, int * ) ;
void fillnode ( int, struct node *, struct node *, int ) ;
void split ( int, struct node *, struct node *, int, int *, struct node ** );


struct node * insert ( int val, struct node *root )
{
    int i ;
    struct node *c;
    int flag ;

    flag = setval ( val, root, &i, &c ) ;
    if ( flag )
    {
        struct node *n =create(treeOrder);
        n -> count = 1 ;
        n -> value[1] = i ;
        n -> child[0] =root ;
        n->child[1]=c;
        return n ;
    }
    return root ;
}

Dani AI

Generated

The compile error and the runtime risk come from two separate issues that already spotted: the creator dereferences an uninitialized pointer, and the node member used to hold children has the wrong type for how it’s later used. Those combine to produce undefined behavior and the “no match for operator=” message when a pointer is being assigned into an array element that is actually a node object.

Why that operator= error appears: when child is declared as a struct node* and set to new node[size], indexing child[0] yields a node object (not a node*). Assigning a node* (like root) into that element is a type mismatch — the compiler would need a conversion from node* to node, which does not exist. Two correct approaches are (A) make child an array of pointers (struct node** child) and allocate new node*[size] so child[0] = root copies the pointer, or (B) keep child as an array of objects and explicitly copy the object (child[0] = *root), understanding that this performs a full node copy.

A safe, minimal fix pattern (illustrative):

struct node {
    int numOfKeys;
    double* value;
    struct node** child;      // array of pointers to children
    int count;
    int i, j;
    node() : numOfKeys(0), value(nullptr), child(nullptr), count(0), i(0), j(1) {}
};

struct node* create_node(int keyNum) {
    int size = 2 * keyNum;
    struct node* p = new struct node();          // allocate the node itself
    p->value = new double[size];                 // key slots
    p->child = new struct node*[size];           // array of child pointers
    for (int k = 0; k < size; ++k) p->child[k] = nullptr;
    return p;
}

Extra tips: always allocate the node before using ptr->..., initialize pointer slots to nullptr, prefer 0-based indexing, and check array bounds. For safer memory management consider std::vector and std::unique_ptr (or std::vector<node*>), and implement destructors to avoid leaks.

Recommended Answers

All 3 Replies

line 24: using an uninitialized pointer ptr You have to allocate memory for it before it can be used.

line 57: child is NOT an array of pointers to structures, but an array of structures. There is a difference. In the structure you need to declare child like this: struct node** child; Notice it has two stars, not one. Then in create() method, allocate the array of pointers like this: child = new struct node*[size];

Thanks! Could you explain to me why it needs to be an array of pointers to structures?

Because line 57 is assigning a pointer to one of the array elements. Can't do that unless child an array of pointers.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.