函数调用时找不到标识符错误

发布于 2024-12-18 16:57:34 字数 829 浏览 2 评论 0原文

我这里有一个程序,可以反转输入字符串的大小写。这是我的 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

一张白纸 2024-12-25 16:57:34

在 main 函数之前添加这一行:

void swapCase (char* name);

int main()
{
   ...
   swapCase(name);    // swapCase prototype should be known at this point
   ...
}

这称为前向声明:编译器在编译函数调用时需要知道函数原型。

Add this line before main function:

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.

傾城如夢未必闌珊 2024-12-25 16:57:34

与您可能习惯的其他语言不同,C++ 中的所有内容都必须先声明才能使用。编译器将从上到下读取源文件,因此当它调用 swapCase 时,它不知道它是什么,因此会出现错误。您可以使用如下行在 main 之前声明您的函数:

void swapCase(char *name);

或者您可以简单地将整个函数移到文件中的 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:

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.

桃气十足 2024-12-25 16:57:34

当编译器在 main() 中遇到对 swapCase 的调用时,它不知道函数 swapCase,因此会报告错误。您可以将 swapCase 的定义移到 main 之上,或者在 main 之上声明 swap case:

void swapCase(char* name);

此外,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';

在这种情况下,写出“我的 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:

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

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.

Good luck learning!

调妓 2024-12-25 16:57:34

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

You have to define void swapCase before the main definition.

极度宠爱 2024-12-25 16:57:34

我在编译现有应用程序时遇到了类似的错误,我在其中添加了新功能......

错误 C3861:“FunctionName”:找不到标识符

  • 解决方案是在使用之前添加方法签名
  • 我必须添加缺少的导入,这导致类无法编译
  • 必须以正确的顺序添加缺少的导入
  • 仔细检查函数名称无论在何处使用,都正确键入

简化示例 1

#include <Windows.h>
#include <iostream>

namespace Test
{
    int FunctionName();
    
    int main()
    {
        int i = FunctionName();
        // do tasks
        return i;
    }
    
    int FunctionName()
    {
        /* my implemented logic with win32 dependencies */
        return -1;
    }
}

如果仅在同一类中需要该函数,则将该函数移到调用它的函数之前。

简化示例 2

#include <Windows.h>
#include <iostream>

namespace Test
{
    int FunctionName()
    {
        /* my implemented logic with win32 dependencies */
        return -1;
    }

    int main()
    {
        int i = FunctionName();
        // do tasks
        return i;
    }
}

I had a similar error compiling an existing application where I added a new function...

error C3861: 'FunctionName': identifier not found

  • The solution was to add the method signature before the usage
  • I had to add missing imports which caused the class to not compile
  • The missing imports had to be added in the correct sequence
  • Double check the function name was correctly typed wherever it is used

Simplified example 1

#include <Windows.h>
#include <iostream>

namespace Test
{
    int FunctionName();
    
    int main()
    {
        int i = FunctionName();
        // do tasks
        return i;
    }
    
    int FunctionName()
    {
        /* my implemented logic with win32 dependencies */
        return -1;
    }
}

If the function is only needed within the same class then move the function before the one that invokes it.

Simplified example 2

#include <Windows.h>
#include <iostream>

namespace Test
{
    int FunctionName()
    {
        /* my implemented logic with win32 dependencies */
        return -1;
    }

    int main()
    {
        int i = FunctionName();
        // do tasks
        return i;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文