2

I am getting below compilation error:

error C3861: 'initNode': identifier not found"

Below is the code:

# include <conio.h>
# include "stdafx.h"
# include <stdlib.h>

struct node{
    node * next;
    int nodeValue;

};

node*createList (int value)  /*Creates a Linked-List*/
{
    node *dummy_node = (node*) malloc(sizeof (node));
    dummy_node->next=NULL;
    dummy_node->nodeValue = value;
    return dummy_node;
}


void addFront (node *head, int num ) /*Adds node to the front of Linked-List*/
{
    node*newNode = initNode(num);   
    newNode->next = NULL;
    head->next=newNode;
    newNode->nodeValue=num;
}

void deleteFront(node*num)   /*Deletes the value of the node from the front*/
{
    node*temp1=num->next;

    if (temp1== NULL) 
    {
        printf("List is EMPTY!!!!");
    }
    else
    {
        num->next=temp1->next;
        free(temp1);
    }

}

void destroyList(node *list)    /*Frees the linked list*/
{
    node*temp;
    while (list->next!= NULL) 
    {
        temp=list;
        list=temp->next;
        free(temp);
    }
    free(list);
}

int getValue(node *list)    /*Returns the value of the list*/
{
    return((list->next)->nodeValue);
}


void printList(node *list)   /*Prints the Linked-List*/
{

    node*currentPosition;
    for (currentPosition=list->next; currentPosition->next!=NULL; currentPosition=currentPosition->next)  
    {`enter code here`
        printf("%d \n",currentPosition->nodeValue);
    }   
    printf("%d \n",currentPosition->nodeValue);

}

node*initNode(int number) /*Creates a node*/
{
    node*newNode=(node*) malloc(sizeof (node));
    newNode->nodeValue=number;
    newNode->next=NULL;
    return(newNode);
}

How to fix this error ?

iammilind
  • 68,093
  • 33
  • 169
  • 336
OOkhan
  • 297
  • 3
  • 8
  • 20

1 Answers1

3

The error is occurring because initNode() is not visible before it is called. To correct place a declaration for initNode(), or move its definition, to before its first use.


The code looks like C but it appears you are using a C++ compiler to compile it (as use of node and not struct node appears to not be causing the compiler to fail, unless you have not reported these errors in your post). If you use a C compiler (which can be easily achieved by having a .c extension on your source file with Visual Studio), you do not need to cast the return value of malloc(). See Incompatibilities Between ISO C and ISO C++ , a link a found provided on answer to the question What issues can I expect compiling C code with a C++ compiler?

Community
  • 1
  • 1
hmjd
  • 120,187
  • 20
  • 207
  • 252
  • I put signature of all funtion at top but now this error is coming "fatal error C1903: unable to recover from previous error(s); stopping compilation" – OOkhan Oct 04 '12 at 08:35
  • @OOkhan, Don't put it before `struct node`. – hmjd Oct 04 '12 at 08:36
  • Solve but another error :( "fatal error LNK1169: one or more multiply defined symbols found" – OOkhan Oct 04 '12 at 08:40
  • If the file you posted is a header file you should move the definitions into the `.c` file and keep declarations in the `.h` file. The question you posted has been answered OOkhan, you would be better to post a new question with the new problem. More people will see it and it much easier to provide a complete answer as an answer and not as comment. – hmjd Oct 04 '12 at 08:43
  • the file i posted is not Header file. it's .cpp file and i am calling these function in another .cpp file. And ok i am going to post my question. – OOkhan Oct 04 '12 at 08:50