c++ – What is an undeclared identifier error and how do I fix it?

c++ – What is an undeclared identifier error and how do I fix it?

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 beginners 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.

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. Youre 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 hasnt seen it before.

If you get such an error in C or C++, it means that you havent told the compiler about the thing you are trying to use. Declarations are often found in header files, so it likely means that you havent included the appropriate header. Of course, it may be that you just havent 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 type X has not been declared with clang will tell you unknown type name X. This is much more useful because you know its trying to interpret X as a type. However, if you have int x = y;, where y is not yet declared, it will tell you use of undeclared identifier y because there is some ambiguity about what exactly y might represent.

c++ – What is an undeclared identifier error and how do I fix it?

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 hasnt been declared you will get an undeclared identifier error.

However, functions are a special case in C (and in C only) in that you dont 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.

Leave a Reply

Your email address will not be published.