LeetCode提交遇‘conflicting types for xxx’编译错误求助
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 anintand 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=c11or similar flags) which rejects implicit declarations. YourlengthOfLongestSubstringcallsmyNoRepeatingbefore its definition, which triggers this error.Truncated/Invalid Code in Submission
The code snippet you shared cuts off mid-definition ofmyNoRepeating:int myNoRepeating(char *substring, in...— it looks like you meantintinstead ofin, 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
Add a Function Prototype
Declare themyNoRepeatingfunction beforelengthOfLongestSubstringso 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; }Ensure Complete, Valid Code
Double-check that you're submitting the full, untruncated version ofmyNoRepeatingwith correct parameter types (fix thatintointif that was a typo).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 likefor(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

