When I try to malloc()
a struct bstree
node, my compiler is reporting an error:
invalid conversion from 'void*' to 'bstree*'
Here is my code:
struct bstree {
int key;
char *value;
struct bstree *left;
struct bstree *right;
};
struct bstree *bstree_create(int key, char *value) {
struct bstree *node;
node = malloc(sizeof (*node));
if (node != NULL) {
node->key = key;
node->value = value;
node->left = NULL;
node->right = NULL;
}
return node;
}
In C++ there is no implicit conversion from type void *
to pointer of other type. You have to specify explicit casting. For example
node = ( struct bstree * )malloc(sizeof (*node));
or
node = static_cast<struct bstree *>( malloc(sizeof (*node)) );
Also in C++ you should use operator new
instead of the C function malloc
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With