error C3861: 'initNode': identifier not fo

2020-04-15 15:24发布

问题:

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 ?

回答1:

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?