What is an 'undeclared identifier' error a

2019-01-01 02:40发布

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)

11条回答
余生请多指教
2楼-- · 2019-01-01 02:52

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.

查看更多
梦该遗忘
3楼-- · 2019-01-01 02:57

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.

查看更多
荒废的爱情
4楼-- · 2019-01-01 02:59

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.

查看更多
柔情千种
5楼-- · 2019-01-01 03:01

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.

查看更多
流年柔荑漫光年
6楼-- · 2019-01-01 03:08

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

查看更多
登录 后发表回答