簡體   English   中英

C:沖突類型錯誤

[英]C: conflicting types error

我想檢查一個鏈接的元素列表,每個元素都包含一個整數,如果一個值已經在里面。

struct ListenElement{
    int wert;
    struct ListenElement *nachfolger;
};

struct ListenAnfang{
    struct ListenElement *anfang;
};

struct ListenAnfang LA;

bool ckeckForInt(int value){
    if (LA.anfang == NULL){
        return 0;
    }
    return checkCurrent(LA.anfang, value);
}

bool checkCurrent(struct ListenElement* check, int value){
    if (check->wert == value){
        return true;
    }
    else if (check->nachfolger != NULL){
        return checkCurrent(check->nachfolger, value);
    }
    else{
        return false;
    }   
}

我得到checkCurrent方法的沖突類型,但找不到它。

checkCurrent() 聲明或定義之前使用,這會導致生成一個返回類型為int的隱式函數聲明(與返回類型為bool的函數的定義不同)。 在第一次使用之前為checkCurrent()添加聲明:

bool checkCurrent(struct ListenElement* check, int value);

bool ckeckForInt(int value){
    if (LA.anfang == NULL){
        return false; /* Changed '0' to 'false'. */
    }
    return checkCurrent(LA.anfang, value);
}

或者在checkForInt()之前移動它的定義。

缺少函數聲明。 在C中,您需要完全按原樣聲明函數。

struct ListenElement{
    int wert;
    struct ListenElement *nachfolger;
};

struct ListenAnfang{
    struct ListenElement *anfang;
};

struct ListenAnfang LA;

//The function declaration !
bool checkCurrent(struct ListenElement* check, int value);

bool ckeckForInt(int value){
    if (LA.anfang == NULL){
        return 0;
    }
    return checkCurrent(LA.anfang, value);
}

bool checkCurrent(struct ListenElement* check, int value){
    if (check->wert == value){
        return true;
    }
    else if (check->nachfolger != NULL){
        return checkCurrent(check->nachfolger, value);
    }
    else{
        return false;
    }   
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM