Compiler Error C2065 in C++ with Solutions

In this tutorial, we will learn about the compiler error C2065 in C++. We look at possible reasons for this error and their corresponding solutions.

The error message displayed for C2065 is:

identifier‘: undeclared identifier

This means that there is some issue with the code and that the compiler is unable to compile the code until this is fixed.

Compiler Error C2065 in C++

Identifiers are the names we give variables or functions in our C++ program. We get the error C2065 when we use such identifiers without first declaring them. Sometimes we may think that we have declared these identifiers, but there is a chance that we have not done so properly. I have shown a simple example below where we get the C2065 error message while compiling the code.

int main()
{
  // uncommenting the following line removes the error
  // int i;

  i = 5;
  return 0;
}

This is because we are using the variable ‘i’ without first declaring it.

The following sections have some common reasons and solutions for the C2065 error message.

Identifier is misspelt

This is a very common mistake that we all make. Sometimes we type the identifier wrong which results in the C2065 error message. Here are a few examples.

int main()
{
  int DOB;
  int students[50];
  
  dob = 32; // we declared 'DOB' but used 'dob'
  student[0] = 437; // we declared 'students' but used 'student'

  return 0;
}

Identifier is unscoped

We must only use identifiers that are properly scoped. In the example given below, we declare ‘a’ inside the ‘if‘ statement. As a result, its scope is restricted to that ‘if‘ block.

int main()
{
  int n = 5;

  if (n > 3)
  {
    int a = 3;
    a++;
  }
  
  // a cannot be used outside its scope
  a = 5;

  return 0;
}

We must also bring library functions and operators into the current scope. A very common example of this is the ‘cout‘ statement which is defined in the ‘std‘ namespace. The following code results in the C2065 error.

#include <iostream>

int main()
{
  cout << "Hello World";

  return 0;
}

This error can be removed by either of the following methods.

#include <iostream>

int main()
{
  // we fully qualify the identifier using the 
  // reuired namespace 
  std::cout << "Hello World";

  return 0;
}

or

#include <iostream>

// we bring the std namespace into the scope
using namespace std;

int main()
{
  cout << "Hello World";

  return 0;
}

Precompiled header is not first

Let us say we have a precompiled header file. All other preprocessor directives must be put after the #include statement of the precompiled header. Thus, the following code having the precompiled header “pch.h” results in a C2065 error.

#include <iostream>
#include "pch.h"

using namespace std;

int main()
{
  cout << "Hello World";

  return 0;
}

We can fix this by moving the including of pch.h to the top as shown below.

#include "pch.h"
#include <iostream>

using namespace std;

int main()
{
  cout << "Hello World";

  return 0;
}

The necessary header file is not included

We need to include header files if we are to use certain identifiers. We see the C2065 error message in the following code as we need to include the string header to use objects of string type.

// Uncomment the following line for the code to work
// #include <string>

using namespace std;

int main()
{
  string s;

  return 0;
}

The closing quote is missing

We need to properly close quotes for the code to work correctly. In the example given below, the C2065 error shows up because the compiler does not properly recognise ‘d’.

int main()
{
  // we need a closing quote (") after 'dog' in the following line
  char c[] = "dog, d[] = "cat";

  d[0] = 'b';

  return 0;
}

Conclusion

In this tutorial, we looked at some possible reasons for the compiler error C2065 in C++. We also learnt about possible solutions. We may obtain this message for other reasons. Here is the link to Microsoft’s documentation for the C2065 error in Visual Studio 2019.

Leave a Reply