错误"函数未在此范围"中声明,即使它是

时间:2018-04-22 15:37:12

标签: c error-handling

根据编译器,函数listLength()未在此作用域中声明。但它在main()函数之前声明 即使我看过其他人的问题,我也看不出这个功能有什么问题。

代码有什么问题?

/* ... */

int listLenght(node* head){
    node *temp = head;
    int length;
    while(temp->next != NULL){
        length++;
        temp = temp->next;
    }
    return length/2;
}


void createList(int length, node *head){
    node *temp = head;
    for(int i = 0; i < length; i++){
        temp->next = (node*) malloc(sizeof(node));
        temp->data = i;
        temp = temp->next;
        temp->next = NULL;
    }
}


...


int main(){
    node *head = (node*) malloc(sizeof(node));
    createList(5, head);
    int a = listLength(head);
}

1 个答案:

答案 0 :(得分:4)

listLength()未定义 listLenght()是。

首先相信编译器,或者试图证明它是错误的,找到这种小错误很容易 即搜索&#34; listLength&#34;并且看到它在你的代码中...除了编译器所抱怨的调用之外的任何地方 或者(马丁詹姆斯的好输入),反过来,采取&#34;未定义的符号&#34;由编译器抱怨并将其粘贴到它所在的地方#34;显然是相同的&#34;定义。 &#34;令人惊讶的是,解决这个问题的频率是多少。 (引用马丁和我完全同意)。如果它确实有帮助,那么有一些很难看到(甚至三次)差异。

&#39; 1&#39; (数字),
&#39;升&#39; (小写&#39; L&#39;),
&#39;我&#39; (大写&#39;我&#39;)
在某些字体中可能会非常相似。