C++报called object is not a function错误如何解决?
报错原因
- 核心问题是代码中多处逻辑判断漏写了逻辑或运算符
||。当编译器解析(userInput >= 'a' && userInput <= 'z') (userInput >= 'A' && userInput <= 'Z')这段代码时,会把前半部分括号的计算结果误识别为要调用的函数名,后半部分括号当成函数传参,因此抛出「called object is not a function or function pointer」的错误。 - 除此之外,元音判断的
vowelVerification赋值行也存在同样的漏写||的问题,只是编译器优先抛出了先检测到的辅音判断行的错误。
修复方案
在所有并列的判断条件之间补充逻辑或运算符||即可,修复后完整代码如下:
#include <stdio.h> #include <stdlib.h> int main() { char userInput; printf("Enter a letter: "); scanf("%c", &userInput); int vowelVerification = (userInput == 'a' || userInput == 'e' || userInput == 'i' || userInput == 'o' || userInput == 'u' || userInput == 'A' || userInput == 'E' || userInput == 'I' || userInput == 'O' || userInput == 'U'); // Checks if the userInput is a vowel. int consonantVerification = (userInput >= 'a' && userInput <= 'z') || (userInput >= 'A' && userInput <= 'Z'); // Checks if the userInput exists in the alphabet. if (vowelVerification) { // The UserInput is a vowel. printf("VOWEL!"); } else if (consonantVerification) { // The UserInput is a consonant. printf("CONSONANT!"); } else { // The UserInput is neither a consonant nor a vowel. printf("INVALID INPUT!"); } }
内容的提问来源于stack exchange,提问作者Sense
相关产品推荐
相关产品推荐

