You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

LeetCode提交遇‘conflicting types for xxx’编译错误求助

Why Your LeetCode Submission Fails Compilation (But Works in CodeBlocks)

Hey there! Let's break down why your code runs fine in CodeBlocks but hits a compilation error on LeetCode, specifically related to your custom myNoRepeating function.

Key Reasons for the Error

  • Missing Function Prototype/Incorrect Order
    In standard C, if you call a function before it's declared or defined, the compiler assumes it returns an int and has unspecified parameters. CodeBlocks' compiler (usually GCC) might allow this implicit declaration in its default relaxed mode, but LeetCode uses strict C standard compliance (like -std=c11 or similar flags) which rejects implicit declarations. Your lengthOfLongestSubstring calls myNoRepeating before its definition, which triggers this error.

  • Truncated/Invalid Code in Submission
    The code snippet you shared cuts off mid-definition of myNoRepeating: int myNoRepeating(char *substring, in... — it looks like you meant int instead of in, and the rest of the function parameters/body are missing. If your actual LeetCode submission has this truncated code, that's a direct syntax error (even CodeBlocks would complain if you tried to compile this incomplete version).

Fixes to Try

  1. Add a Function Prototype
    Declare the myNoRepeating function before lengthOfLongestSubstring so the compiler knows its signature when it's called:

    // Function prototype first
    int myNoRepeating(char *substring, int step, char newChar);
    
    int lengthOfLongestSubstring(char* s) {
        int longestSub = 1;
        for(int startIndex = 0, step = 1; startIndex + step < strlen(s); ){
            if(myNoRepeating(s+startIndex, step, s[startIndex+step])){
                step++;
                longestSub = step;
            } else {
                startIndex++;
            }
        }
        return longestSub;
    }
    
    // Full definition of myNoRepeating here
    int myNoRepeating(char *substring, int step, char newChar) {
        // Your logic to check for repeating characters goes here
        // Example placeholder (replace with your actual code):
        for(int i = 0; i < step; i++){
            if(substring[i] == newChar) return 0;
        }
        return 1;
    }
    
  2. Ensure Complete, Valid Code
    Double-check that you're submitting the full, untruncated version of myNoRepeating with correct parameter types (fix that in to int if that was a typo).

  3. Align with Strict C Standards
    LeetCode's compiler enforces strict C rules. For example, if you're using an older C standard (pre-C99), variable declarations like for(int startIndex = 0... need to be moved to the top of the function. Though most modern compilers support C99, it's worth verifying if that's causing any issues.

内容的提问来源于stack exchange,提问作者bluebluesky

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 08:54:59