What are undeclared identifier errors? What are common causes and how do I fix them?
Example error texts:
error C2065: 'cout' : undeclared identifier
'cout' undeclared (first use in this function)
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.
"Undeclared identifier" means that you tried to use an identifier that wasn't declared: that is, an identifier that it doesn't know about.
"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.
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.
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:
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.
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; }
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. }
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With