函数调用时找不到标识符错误
我这里有一个程序,可以反转输入字符串的大小写。这是我的 .cpp 文件中的代码,我使用的是 Visual Studio C++ IDE。我不确定头文件中需要什么,或者是否需要一个头文件才能完成这项工作。
我的函数调用 swapCase 出错。由于某种我不确定的原因,Main 没有看到 swapCase。
#include <cctype>
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
char name[30];
cout<<"Enter a name: ";
cin.getline(name, 30);
swapCase(name);
cout<<"Changed case is: "<< name <<endl;
_getch();
return 0;
}
void swapCase (char* name)
{
for(int i=0;name[i];i++)
{
if ( name[i] >= 'A' && name[i] <= 'Z' )
name[i] += 32; //changing upper to lower
else if( name[i] >= 'a' && name[i] <= 'z')
name[i] -= 32; //changing lower to upper
}
}
任何其他语法或语义提示都值得赞赏。
I have a program here where I invert the case of an entered string. This is the code in my .cpp file and I am using Visual Studio C++ IDE. I am not sure what I need in a header file or if I need one to make this work.
Error with my function call swapCase. Main does not see swapCase for some reason that I'm not sure of.
#include <cctype>
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
char name[30];
cout<<"Enter a name: ";
cin.getline(name, 30);
swapCase(name);
cout<<"Changed case is: "<< name <<endl;
_getch();
return 0;
}
void swapCase (char* name)
{
for(int i=0;name[i];i++)
{
if ( name[i] >= 'A' && name[i] <= 'Z' )
name[i] += 32; //changing upper to lower
else if( name[i] >= 'a' && name[i] <= 'z')
name[i] -= 32; //changing lower to upper
}
}
Any other tips for syntax or semantics is appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在 main 函数之前添加这一行:
这称为前向声明:编译器在编译函数调用时需要知道函数原型。
Add this line before main function:
This is called forward declaration: compiler needs to know function prototype when function call is compiled.
与您可能习惯的其他语言不同,C++ 中的所有内容都必须先声明才能使用。编译器将从上到下读取源文件,因此当它调用
swapCase
时,它不知道它是什么,因此会出现错误。您可以使用如下行在 main 之前声明您的函数:或者您可以简单地将整个函数移到文件中的 main 之前。不要担心看似最重要的函数(main)位于文件底部。在 C 或 C++ 中这样做很常见。
Unlike other languages you may be used to, everything in C++ has to be declared before it can be used. The compiler will read your source file from top to bottom, so when it gets to the call to
swapCase
, it doesn't know what it is so you get an error. You can declare your function ahead of main with a line like this:or you can simply move the entirety of that function ahead of main in the file. Don't worry about having the seemingly most important function (main) at the bottom of the file. It is very common in C or C++ to do that.
当编译器在 main() 中遇到对 swapCase 的调用时,它不知道函数 swapCase,因此会报告错误。您可以将 swapCase 的定义移到 main 之上,或者在 main 之上声明 swap case:
此外,swapCase 中的 32 会让读者停下来思考。评论有帮助! 会更加清晰
在这种情况下,写出“我的 if 测试中的构造是个人风格的问题” 。你的很好。最主要的是修改 name[i] 的方法——使用 'a' 与 'A' 的差异使得发生的事情更加明显,并且没有人会怀疑 '32' 是否真的正确。
祝学习顺利!
At the time the compiler encounters the call to swapCase in main(), it does not know about the function swapCase, so it reports an error. You can either move the definition of swapCase above main, or declare swap case above main:
Also, the 32 in swapCase causes the reader to pause and wonder. The comment helps! In this context, it would add clarity to write
The construction in my if-tests is a matter of personal style. Yours were just fine. The main thing is the way to modify name[i] -- using the difference in 'a' vs. 'A' makes it more obvious what is going on, and nobody has to wonder if the '32' is actually correct.
Good luck learning!
您必须在主定义之前定义 void swapCase。
You have to define void swapCase before the main definition.
我在编译现有应用程序时遇到了类似的错误,我在其中添加了新功能......
简化示例 1
如果仅在同一类中需要该函数,则将该函数移到调用它的函数之前。
简化示例 2
I had a similar error compiling an existing application where I added a new function...
Simplified example 1
If the function is only needed within the same class then move the function before the one that invokes it.
Simplified example 2