简体   繁体   English

函数调用中未找到标识符错误

[英]Identifier not found error on function call

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.这是我的 .cpp 文件中的代码,我使用的是 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.我的函数调用 swapCase 出错。 Main does not see swapCase for some reason that I'm not sure of.由于某种我不确定的原因,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.任何其他有关语法或语义的提示表示赞赏。

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.这称为前向声明:编译器在编译函数调用时需要知道函数原型。

Unlike other languages you may be used to, everything in C++ has to be declared before it can be used.与您可能习惯的其他语言不同,C++ 中的所有内容都必须先声明才能使用。 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.编译器会从上到下读取你的源文件,所以当它调用swapCase ,它不知道它是什么,所以你会得到一个错误。 You can declare your function ahead of main with a line like this:您可以使用如下一行在 main 之前声明您的函数:

void swapCase(char *name);

or you can simply move the entirety of that function ahead of main in the file.或者您可以简单地将整个函数移动到文件中的 main 之前。 Don't worry about having the seemingly most important function (main) at the bottom of the file.不要担心文件底部有看似最重要的函数(main)。 It is very common in C or C++ to do that.在 C 或 C++ 中这样做很常见。

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.编译器在main()中遇到调用swapCase时,并不知道swapCase函数,所以报错。 You can either move the definition of swapCase above main, or declare swap case above main:您可以将 swapCase 的定义移到 main 之上,或者在 main 之上声明swap case:

void swapCase(char* name);

Also, the 32 in swapCase causes the reader to pause and wonder.此外,swapCase 中的 32 会导致读者暂停和思考。 The comment helps!评论有帮助! In this context, it would add clarity to write在这种情况下,它会增加写作的清晰度

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.我的 if 测试中的构造是个人风格的问题。 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.最重要的是修改 name[i] 的方法——使用 'a' 和 'A' 的不同使得发生的事情更加明显,没有人需要怀疑 '32' 是否真的正确。

Good luck learning!祝学习顺利!

您必须在主定义之前定义 void swapCase。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM