invalid conversion from 'void' to 'LNODE'

Oct 8, 2008 at 2:29pm
Hi guys, this is my first time posting. I got this code from a book in my school library. I wanted to check it out but it won't compile. An error of "invalid conversion from 'void' to 'LNODE' " keeps on appearing... Can someone tell me what's wrong?

Here's the code:

#include <iostream>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

typedef struct node
{
char data;
struct node *lchild;
struct node *rchild;
} LNODE;

int number_of_nodes;

void build_tree(LNODE **ptr);
void pre_order(LNODE *ptr);
void in_order(LNODE *ptr);
void post_order(LNODE *ptr);

int main()
{
LNODE *tree;

build_tree(&tree);
cout << "Pre-Order Traversal => ";
pre_order(tree);
cout << "\nIn-Order Traversal => ";
in_order(tree);
cout << "\nPost-Order Traversal => ";
post_order(tree);
}

void build_tree(LNODE **ptr)
{
LNODE *n1, *n2, *n3, *n4, *n5;
n1 = malloc(sizeof(LNODE));
n2 = malloc(sizeof(LNODE));
n3 = malloc(sizeof(LNODE));
n4 = malloc(sizeof(LNODE));
n5 = malloc(sizeof(LNODE));
n1->data = '*';
n1->lchild = n2;
n1->rchild = n3;
n2->data = '+';
n2->lchild = n4;
n2->rchild = n5;
n3->data = 'C';
n3->lchild = NULL;
n3->rchild = NULL;
n4->data = 'A';
n4->lchild = NULL;
n4->rchild = NULL;
n5->data = 'B';
n5->lchild = NULL;
n5->rchild = NULL;
*ptr = n1;
}

void count_nodes(LNODE *ptr)
{
if (ptr != NULL)
{
number_of_nodes++;
count_nodes(ptr->lchild);
count_nodes(ptr->rchild);
}
}

Thanks in advance!
Oct 8, 2008 at 2:50pm
I'm assuming the error occurs 5 times, once for each call to malloc.

malloc returns a void*, which is being assigned to an LNODE*.
This cannot be done without an explicit cast.

1
2
n1 = (LNODE*)malloc( sizeof( LNODE ) );
// etc. 

Oct 8, 2008 at 7:14pm
thanks!
Topic archived. No new replies allowed.