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

Discussion in 'C++' started by Joah Senegal, Sep 14, 2006.

  1. Joah Senegal

    Joah Senegal Guest

    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!
    Joah Senegal, Sep 14, 2006
    #1
    1. Advertising

  2. Joah Senegal

    David Harmon Guest

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


    void* conversion is automatic in C; in C++ you have to ask for it.

    node *n = (node *) malloc(sizeof(node));
    David Harmon, Sep 14, 2006
    #2
    1. Advertising

  3. Joah Senegal

    Default User Guest

    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
    Default User, Sep 14, 2006
    #3
  4. Joah Senegal

    Earl Purple Guest

    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.
    Earl Purple, Sep 15, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ollej Reemt
    Replies:
    7
    Views:
    518
    Jack Klein
    Apr 22, 2005
  2. Stig Brautaset

    `void **' revisited: void *pop(void **root)

    Stig Brautaset, Oct 25, 2003, in forum: C Programming
    Replies:
    15
    Views:
    786
    The Real OS/2 Guy
    Oct 28, 2003
  3. Replies:
    5
    Views:
    824
    S.Tobias
    Jul 22, 2005
  4. philwozza
    Replies:
    3
    Views:
    3,517
    philwozza
    May 13, 2006
  5. Replies:
    1
    Views:
    401
    Victor Bazarov
    May 23, 2007
Loading...

Share This Page