【问题标题】:C: conflicting types errorC:冲突类型错误
【发布时间】:2013-04-24 09:49:59
【问题描述】:

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

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

【问题讨论】:

    标签: c list struct type-conversion


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      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()之前。

      【讨论】:

        猜你喜欢
        • 2014-03-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-02-07
        • 2013-03-25
        • 2023-03-31
        • 2012-10-04
        相关资源
        最近更新 更多