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)
In C and C++ all names have to be declared before they are used. If you try to use the name of a variable or a function that hasn't been declared you will get an "undeclared identifier" error.
However, functions are a special case in C (and in C only) in that you don't have to declare them first. The C compiler will the assume the function exists with the number and type of arguments as in the call. If the actual function definition does not match that you will get another error. This special case for functions does not exist in C++.
You fix these kind of errors by making sure that functions and variables are declared before they are used. In the case of
printf
you need to include the header file<stdio.h>
(or<cstdio>
in C++).For standard functions, I recommend you check e.g. this reference site, and search for the functions you want to use. The documentation for each function tells you what header file you need.
Consider a similar situation in conversation. Imagine your friend says to you, "Bob is coming over for dinner," and you have no idea who Bob is. You're going to be confused, right? Your friend should have said, "I have a work colleague called Bob. Bob is coming over for dinner." Now Bob has been declared and you know who your friend is talking about.
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.
If you get such an error in C or C++, it means that you haven't told the compiler about the thing you are trying to use. Declarations are often found in header files, so it likely means that you haven't included the appropriate header. Of course, it may be that you just haven't remembered to declare the entity at all.
Some compilers give more specific errors depending on the context. For example, attempting to compile
X x;
where the typeX
has not been declared with clang will tell you "unknown type nameX
". This is much more useful because you know it's trying to interpretX
as a type. However, if you haveint x = y;
, wherey
is not yet declared, it will tell you "use of undeclared identifiery
" because there is some ambiguity about what exactlyy
might represent.I had the same problem with a custom class, which was defined in a namespace. I tried to use the class without the namespace, causing the compiler error "identifier "MyClass" is undefined". Adding
or using the class like
solved the problem.
These error meassages
mean that you use name
printf
but the compiler does not see where the name was declared and accordingly does not know what it means.Any name used in a program shall be declared before its using. The compiler has to know what the name denotes.
In this particular case the compiler does not see the declaration of name
printf
. As we know (but not the compiler) it is the name of standard C function declared in header<stdio.h>
in C or in header<cstdio>
in C++ and placed in standard (std::
) and global (::
) (not necessarily) name spaces.So before using this function we have to provide its name declaration to the compiler by including corresponding headers.
For example C:
C++:
Sometimes the reason of such an error is a simple typo. For example let's assume that you defined function
PrintHello
but in main you made a typo and instead of
PrintHello
you typedprintHello
with lower case letter 'p'.In this case the compiler will issue such an error because it does not see the declaration of name
printHello
.PrintHello
andprintHello
are two different names one of which was declared and other was not declared but used in the body of mainAnother possible situation: accessing parent (a template class) member in a template class.
Fix method: using the parent class member by its full name (by prefixing
this->
orparentClassName::
to the name of the member).see: templates: parent class member variables not visible in inherited class
A C++ identifier is a name used to identify a variable, function, class, module, or any other user-defined item. In C++ all names have to be declared before they are used. If you try to use the name of a such that hasn't been declared you will get an "undeclared identifier" compile-error.
According to the documentation, the declaration of printf() is in cstdio i.e. you have to include it, before using the function.