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)
Most of the time, if you are very sure you imported the library in question, Visual Studio will guide you with IntelliSense.
Here is what worked for me:
Make sure that
#include "stdafx.h"
is declared first, that is, at the top of all of your includes.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
To fix it, we must include the header:
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:
Incorrect scope
For example, this code would give an error, because you need to use
std::string
:Use before declaration
g
has not been declared before its first use. To fix it, either move the definition ofg
beforef
:Or add a declaration of
g
beforef
: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:The
#include <iostream>
would be ignored. You need to move it below:Feel free to edit this answer.
Every undeclared variable in c error comes because the compiler is not able to find it in the project. One can include the external (header) file of the library in which the variable is defined. Hence in your question, you require
<stdio.h>
, that is a standard input output file, which describes printf(), functionality.According to the documentation, the declaration of fprintf() is in i.e. you have to include it, before using the function.
It is like Using the function without declaring it. header file will contain the function printf(). Include the header file in your program this is the solution for that. Some user defined functions may also through error when not declared before using it. If it is used globally no probs.
Check if you are importing the same packages in your .m and in your .h Example given: I had this very problem with the init method and it was caused by missing the "#import " on the .m file