繁体   English   中英

编译错误“未声明的标识符” C

[英]Compiling Error 'undeclared identifier' C

我的代码几乎完成了,但是它给了我这个错误:' curtemp: undeclared identifier '。 和' prevtemp: undeclared identifier '和' missing ';' before type missing ';' before type (最后一个错误是在“ float curtemp = current-> temp;”行上。我不知道我的代码有什么问题。我正在尝试从双向链表中删除某些元素,因为其中的温度是元素比上一个元素的温度高5或小5。

这是我的.c文件:

void remove_error(Dlist *list){

    DlistElmt *current;

    current = list->head; 
    //initializes ints for comparison
    float curtemp = current->temp;
    float prevtemp = current->temp;
    //now moves current to next data
    current = current -> next;

    //begins while loop for comparison of data
    while(current != NULL){
        curtemp = current -> temp;

        if((curtemp >= (prevtemp +5)) || (curtemp <= (prevtemp-5))){
            //removes current from the list
            dlist_remove(list, current);

            current = current->next;

        }
    }

 }

这是我的struct元素文件:

typedef struct DlistElmt_ {

    int hour;
    int min;
    float temp;

    struct DlistElmt_ *prev;
    struct DlistElmt_ *next;

 } DlistElmt;

对于C89或C90,预计必须在块(函数或本地块)的开头声明变量。 但是,对于C99,此限制不适用。 因此,很有可能必须采用上述Karthik T&japreiss的建议。

您可以尝试通过将float变量的声明仅移动到函数的开头,然后再分配它们。

您的代码不是ISO标准C89,并且我怀疑您有C89编译器(如Visual C)。 您必须重新编码一些行:

DlistElmt *current; 
float curtemp, prevtem;

current = list->head;  
//initializes ints for comparison 
curtemp = prevtemp = current->temp; 

做到这一点。

它是gcc C89扩展,C99标准以及C ++功能,允许在块的中间而不是仅在顶部声明。 这使许多人在遇到“纯” ISO C89编译器时感到困惑。

您的逻辑中似乎还缺少某些东西。 一旦设置, prevtemp将永远不会更新。 如果prev表示上prev ,那么我想您想在循环内进行:

prevtem = curtemp;
curtemp = current -> temp;          

在这种情况下,某些初始化步骤是多余的。 考虑阅读有关“编程科学”的Gries,以了解如何避免此类错误。

我的猜测是您不包括已定义的文件DlistElmt ,但是您已经包含了DlistElmt的声明,因此编译器知道有DlistElmt的东西,因此可以使用指向它的指针,但是当您尝试对DlistElmt的内容进行操作时编译器无法执行任何操作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM