【问题标题】:I have an error in my code, undeclared identifier我的代码中有错误,未声明的标识符
【发布时间】:2020-05-11 09:29:33
【问题描述】:

编译时提示错误

error: use of undeclared identifier 'isVowel'; did you mean 'islower'?

#include <iostream>

#include <cstring>

using namespace std;

int main() {

char word[50];

int num = 0;

cout << "Enter word: ";

cin.getline(word,50);

for(int i=0; word[i]; ++i){

if(isVowel(word[i]))

++num;

}

cout<<"The total number of vowels are "<<num<<endl;



}

bool isVowel(char c){

if(c=='a' || c=='A')

return true;

else if(c=='e' || c=='E')

return true;

else if(c=='i' || c=='I')

return true;

else if(c=='o' || c=='O')

return true;

else if(c=='u' || c=='U')

return true;

return false;

}

【问题讨论】:

标签: c++ undeclared-identifier


【解决方案1】:

您需要在使用函数之前对其进行原型设计。否则编译器不知道它存在:

bool isVowel(char c); // A prototype of the function you will later call

int main () {
    //... Whatever code you're doing....


}

bool isVowel(char c) {
   // Actually implement it here.

}

【讨论】:

  • 意见:我的偏好是在首次使用之前定义函数。我讨厌必须在代码中进行两项更改。部分原因是我很懒惰,另一个原因是当您通过仅更改两者中的一个来注入错误时会很糟糕。
  • 哦,好吧,这是有道理的!非常感谢!
猜你喜欢
  • 2020-12-19
  • 1970-01-01
  • 2013-03-12
  • 1970-01-01
  • 2013-01-22
  • 2011-10-25
  • 2011-02-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多