C++ 函数调用中未找到标识符错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8329103/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Identifier not found error on function call
提问by KRB
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.
我在这里有一个程序,可以反转输入字符串的大小写。这是我的 .cpp 文件中的代码,我使用的是 Visual Studio C++ IDE。我不确定我在头文件中需要什么,或者我是否需要一个来完成这项工作。
Error with my function call swapCase. Main does not see swapCase for some reason that I'm not sure of.
我的函数调用 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
}
}
Any other tips for syntax or semantics is appreciated.
任何其他有关语法或语义的提示表示赞赏。
回答by Alex F
Add this line before main function:
在 main 函数之前添加这一行:
void swapCase (char* name);
int main()
{
...
swapCase(name); // swapCase prototype should be known at this point
...
}
This is called forward declaration: compiler needs to know function prototype when function call is compiled.
这称为前向声明:编译器在编译函数调用时需要知道函数原型。
回答by Michael Kristofik
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:
与您可能习惯的其他语言不同,C++ 中的所有内容都必须先声明才能使用。编译器将从上到下读取您的源文件,因此当它调用 时swapCase
,它不知道它是什么,因此您会收到错误消息。您可以使用如下一行在 main 之前声明您的函数:
void swapCase(char *name);
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 之前。不要担心文件底部有看似最重要的函数(main)。在 C 或 C++ 中这样做很常见。
回答by Kevin Hopps
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 definitionof swapCase above main, or declareswap case above main:
编译器在main()中遇到调用swapCase时,并不知道swapCase函数,所以报错。您可以将swapCase的定义移到 main 之上,或者在 main 之上声明swap case:
void swapCase(char* name);
Also, the 32 in swapCase causes the reader to pause and wonder. The comment helps! In this context, it would add clarity to write
此外,swapCase 中的 32 会导致读者暂停和思考。评论有帮助!在这种情况下,它会增加写作的清晰度
if ('A' <= name[i] && name[i] <= 'Z')
name[i] += 'a' - 'A';
else if ('a' <= name[i] && name[i] <= 'z')
name[i] += 'A' - 'a';
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.
我的 if 测试中的构造是个人风格的问题。你的就好了。最重要的是修改 name[i] 的方法——使用 'a' 和 'A' 的不同使得发生的事情更加明显,没有人需要怀疑 '32' 是否真的正确。
Good luck learning!
祝学习顺利!
回答by v01d
You have to define void swapCase before the main definition.
您必须在主定义之前定义 void swapCase。