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

J

Joah Senegal

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!
 
D

David Harmon

On Thu, 14 Sep 2006 13:39:27 +0200 in comp.lang.c++, "Joah Senegal"
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));
 
D

Default User

Joah said:
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
 
E

Earl Purple

Default said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top