473,324 Members | 2,567 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

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 9431
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Thomas Barth | last post by:
Hi, I'm new to windows programming and still reading a book about windows-programming with C++. I copied the following code from the book into my ide (Eclipse/CDT) to comprehend the code, but two...
14
by: Richard G. Riley | last post by:
Would it be wrong to use "implicit casting" instead of the standards "implicit conversion" when talking about implicit conversions between certain data types. The standard mentions "explicit...
3
by: philwozza | last post by:
Im trying to implement a THREAD class that encapsulates a posix thread. Here is an outline of my THREAD class. class THREAD { public: // returns 1 if thread sucessfully started int...
11
by: Martin Jørgensen | last post by:
Hi, I'm using this alloc_mem-function: - - - - - - - - - - - - - - - - - - - - - - - - void *alloc_mem (size_t num_elems, size_t elem_size, char *filename, int line, size_t *total_mem) {
2
by: tkirankumar | last post by:
Hi all, uname -a SunOS cbmrsd1a1 5.10 Generic_118833-17 sun4us sparc FJSV,GPUZC-M g++ -v Reading specs from /usr/local/lib/gcc-lib/sparc-sun-solaris2.10/3.3.2/specs Configured with:...
10
by: haomiao | last post by:
I want to implement a common list that can cantain any type of data, so I declare the list as (briefly) --------------------------------------- struct list { int data_size; int node_num;...
3
by: fazulu deen | last post by:
Hi all, For the following code : file_ptr = fopen("pass_fail.txt", "a"); // error line 393 fdisplay(file_ptr, "Test Passed"); fclose(file_ptr);
1
by: Nozdormu | last post by:
Hi guys, I have a quick question. The language I'm using is C. The question I'm tackling: Write a program that (1) defines a 1D array with ten int elements, and (2) sets the values of the...
3
by: Tex08 | last post by:
I have hit a roadblock on a class project. Trying to implement the composite pattern with separate classes to represent a boolean expression. My literal class will not compile (g++, required...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.