468,496 Members | 1,796 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,496 developers. It's quick & easy.

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

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
3 7924
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
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

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.

Similar topics

14 posts views Thread by Richard G. Riley | last post: by
3 posts views Thread by philwozza | last post: by
11 posts views Thread by Martin Jørgensen | last post: by
10 posts views Thread by haomiao | last post: by
3 posts views Thread by fazulu deen | last post: by
3 posts views Thread by gieforce | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.