Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is an 'undeclared identifier' error and how do I fix it?

What are undeclared identifier errors? What are common causes and how do I fix them?

Example error texts:

  • For the Visual Studio compiler: error C2065: 'cout' : undeclared identifier
  • For the GCC compiler: 'cout' undeclared (first use in this function)
like image 887
sashoalm Avatar asked Mar 05 '14 11:03

sashoalm


People also ask

How do you fix an undeclared identifier error?

How to Fix a “use of undeclared identifier” compilation error in C++ VARIABLE NOT DECLARED: When we are using a variable sometimes we might forget to declare it. We keep on writing the code using the variable without declaring it. To fix this, we simply need to declare the variable before using it.

What does undeclared identifier mean in Pinescript?

"Undeclared identifier" means that you tried to use an identifier that wasn't declared: that is, an identifier that it doesn't know about.

What does undeclared identifier mean in Delphi?

"Undeclared identifier" means that Delphi cannot find the declaration that tells it what showmassage is, so it highlights it as an item that hasn't been declared.

What is undefined identifier?

The compiler emits an 'undeclared identifier' error when you have attempted to use some identifier (what would be the name of a function, variable, class, etc.) and the compiler has not seen a declaration for it. That is, the compiler has no idea what you are referring to because it hasn't seen it before.


1 Answers

They most often come from forgetting to include the header file that contains the function declaration, for example, this program will give an 'undeclared identifier' error:

Missing header

int main() {     std::cout << "Hello world!" << std::endl;     return 0; } 

To fix it, we must include the header:

#include <iostream> int main() {     std::cout << "Hello world!" << std::endl;     return 0; } 

If you wrote the header and included it correctly, the header may contain the wrong include guard.

To read more, see http://msdn.microsoft.com/en-us/library/aa229215(v=vs.60).aspx.

Misspelled variable

Another common source of beginner's error occur when you misspelled a variable:

int main() {     int aComplicatedName;     AComplicatedName = 1;  /* mind the uppercase A */     return 0; } 

Incorrect scope

For example, this code would give an error, because you need to use std::string:

#include <string>  int main() {     std::string s1 = "Hello"; // Correct.     string s2 = "world"; // WRONG - would give error. } 

Use before declaration

void f() { g(); } void g() { } 

g has not been declared before its first use. To fix it, either move the definition of g before f:

void g() { } void f() { g(); } 

Or add a declaration of g before f:

void g(); // declaration void f() { g(); } void g() { } // definition 

stdafx.h not on top (VS-specific)

This is Visual Studio-specific. In VS, you need to add #include "stdafx.h" before any code. Code before it is ignored by the compiler, so if you have this:

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

The #include <iostream> would be ignored. You need to move it below:

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

Feel free to edit this answer.

like image 168
7 revs, 4 users 69% Avatar answered Sep 24 '22 18:09

7 revs, 4 users 69%