【问题标题】:error C3861: 'initNode': identifier not found错误 C3861:“initNode”:找不到标识符
【发布时间】:2012-10-04 08:20:15
【问题描述】:

我遇到以下编译错误:

错误 C3861:“initNode”:未找到标识符”

下面是代码:

# 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);
}

如何解决这个错误?

【问题讨论】:

    标签: c++ c compiler-errors


    【解决方案1】:

    发生错误是因为initNode() 在被调用之前不可见。 更正initNode() 的声明,或将其定义移至第一次使用之前。


    代码看起来像 C,但您似乎正在使用 C++ 编译器来编译它(因为使用 node 而不是 struct node 似乎不会导致编译器失败,除非您没有在你的帖子)。如果您使用 C 编译器(可以通过使用 Visual Studio 在源文件上添加 .c 扩展名轻松实现),则无需转换 malloc() 的返回值。请参阅Incompatibilities Between ISO C and ISO C++ ,这是在回答问题What issues can I expect compiling C code with a C++ compiler? 时提供的链接

    【讨论】:

    • 我把所有功能的签名放在最上面,但现在这个错误来了“致命错误C1903:无法从以前的错误中恢复;停止编译”
    • @OOkhan,不要放在struct node之前。
    • 解决但另一个错误:(“致命错误 LNK1169:找到一个或多个多重定义的符号”
    • 如果您发布的文件是头文件,您应该将定义移动到.c 文件中,并将声明保留在.h 文件中。您发布的问题已得到回答 OOkhan,您最好发布一个带有新问题的新问题。更多的人会看到它,并且更容易提供完整的答案作为答案而不是评论。
    • 我发布的文件不是头文件。它是 .cpp 文件,我在另一个 .cpp 文件中调用这些函数。好的,我将发布我的问题。
    猜你喜欢
    • 2013-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-31
    • 1970-01-01
    • 2013-04-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多