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

2021-12-31 00:00:00 compiler-errors c++

我在这里有一个程序,可以反转输入字符串的大小写.这是我的 .cpp 文件中的代码,我使用的是 Visual Studio C++ IDE.我不确定头文件中需要什么,或者我是否需要一个来完成这项工作.

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.

我的函数调用 swapCase 出错.由于某种我不确定的原因,Main 没有看到 swapCase.

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.

推荐答案

在main函数前添加这一行:

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.

相关文章