Identifier not found error on function call
Asked Answered
R

5

68

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.

Ramadan answered 30/11, 2011 at 16:11 Comment(1)
It's very dangerous to use a fixed-size array for user input - if I enter 40 characters then the program might crash, or worse. You should make name a std::string, and read it as getline(cin, name).Interceptor
R
108

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.

Radiotherapy answered 30/11, 2011 at 16:13 Comment(3)
Or simply move the defintion of swapCase before main.Ideograph
So the callee has to be above the caller? This is hilarious! That is so ancient and lazy of the compiler developers.Shugart
@MacGyver that has nothing to do with the compiler. That's mandated by the C++ standardCiaracibber
M
26

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.

Mesnalty answered 30/11, 2011 at 16:19 Comment(0)
F
10

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!

Feces answered 30/11, 2011 at 16:20 Comment(0)
I
1

You have to define void swapCase before the main definition.

Ikkela answered 30/11, 2011 at 16:13 Comment(0)
A
0

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;
    }
}
Aspiration answered 9/3 at 15:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.