By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,044 Members | 1,287 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,044 IT Pros & Developers. It's quick & easy.

invalid conversion from `void*' to `node*'

P: n/a
Hello all,

I've them following C-code wich I need to compile with a c++ compiler. This
is only one error, but I'm trying to fix it for days now, without any
result. It's about the following line:

node *n = malloc(sizeof(node));

This is the complete code of the method

node *list_add(node **p, int i) {
node *n = malloc(sizeof(node));
n->next = *p;
*p = n;
n->data = i;
return n;
}

Anyone has a solution for this problem?

Thanks!
Sep 14 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Thu, 14 Sep 2006 13:39:27 +0200 in comp.lang.c++, "Joah Senegal"
<bl****@hva.nlwrote,
>Hello all,

I've them following C-code wich I need to compile with a c++ compiler. This
is only one error, but I'm trying to fix it for days now, without any
result. It's about the following line:

node *n = malloc(sizeof(node));
void* conversion is automatic in C; in C++ you have to ask for it.

node *n = (node *) malloc(sizeof(node));

Sep 14 '06 #2

P: n/a
Joah Senegal wrote:
Hello all,

I've them following C-code wich I need to compile with a c++
compiler. This is only one error, but I'm trying to fix it for days
now, without any result. It's about the following line:

node *n = malloc(sizeof(node));

This is the complete code of the method

node *list_add(node **p, int i) {
node *n = malloc(sizeof(node));
n->next = *p;
*p = n;
n->data = i;
return n;
}

Anyone has a solution for this problem?
In C++, you should replace malloc() calls with new.

node *n = new node;
Usually better yet is to get rid of hand-rolled linked-lists, and use
the standard list container.

Brian
Sep 14 '06 #3

P: n/a

Default User wrote:
Joah Senegal wrote:
Hello all,

I've them following C-code wich I need to compile with a c++
compiler. This is only one error, but I'm trying to fix it for days
now, without any result. It's about the following line:

node *n = malloc(sizeof(node));

This is the complete code of the method

node *list_add(node **p, int i) {
node *n = malloc(sizeof(node));
n->next = *p;
*p = n;
n->data = i;
return n;
}

Anyone has a solution for this problem?

In C++, you should replace malloc() calls with new.

node *n = new node;
Usually better yet is to get rid of hand-rolled linked-lists, and use
the standard list container.
but given the whole code is written in C and he may want to compile it
in C, the C-style cast is probably the best option, as it works in C as
well.

When he wants to write a C++ version, a whole rewrite (using std::list
or some other container) would be suggested.

Sep 15 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.