C:冲突类型错误

时间:2013-04-30 12:58:54

标签: c list struct type-conversion

如果值已经在里面,我想检查一个链接的元素列表,每个元素都包含一个整数。

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方法的冲突类型,但找不到它。

2 个答案:

答案 0 :(得分:4)

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()之前移动其定义。

答案 1 :(得分:4)

缺少函数声明。 在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;
    }   
}